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"
39#include "ruby_assert.h"
42VALUE rb_cArray_empty_frozen;
71#define ARY_DEFAULT_SIZE 16
72#define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
73#define SMALL_ARRAY_LEN 16
77should_be_T_ARRAY(
VALUE ary)
82#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
83#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
84#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
85 RARRAY(a)->as.heap.aux.capa)
87#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
88#define ARY_EMBED_LEN(a) \
89 (RUBY_ASSERT(ARY_EMBED_P(a)), \
90 (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
91 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
92#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
94#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
95 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
97#define FL_SET_EMBED(a) do { \
98 RUBY_ASSERT(!ARY_SHARED_P(a)); \
99 FL_SET((a), RARRAY_EMBED_FLAG); \
103#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
104#define FL_SET_SHARED(ary) do { \
105 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
106 FL_SET((ary), RARRAY_SHARED_FLAG); \
108#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
110#define ARY_SET_PTR(ary, p) do { \
111 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
112 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
113 RARRAY(ary)->as.heap.ptr = (p); \
115#define ARY_SET_EMBED_LEN(ary, n) do { \
117 RUBY_ASSERT(ARY_EMBED_P(ary)); \
118 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
119 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
121#define ARY_SET_HEAP_LEN(ary, n) do { \
122 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
123 RARRAY(ary)->as.heap.len = (n); \
125#define ARY_SET_LEN(ary, n) do { \
126 if (ARY_EMBED_P(ary)) { \
127 ARY_SET_EMBED_LEN((ary), (n)); \
130 ARY_SET_HEAP_LEN((ary), (n)); \
132 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
134#define ARY_INCREASE_PTR(ary, n) do { \
135 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
136 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
137 RARRAY(ary)->as.heap.ptr += (n); \
139#define ARY_INCREASE_LEN(ary, n) do { \
140 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
141 if (ARY_EMBED_P(ary)) { \
142 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
145 RARRAY(ary)->as.heap.len += (n); \
149#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
150 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
151#define ARY_SET_CAPA(ary, n) do { \
152 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
153 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
154 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
155 RARRAY(ary)->as.heap.aux.capa = (n); \
158#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
159#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
160 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
161 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
162 RUBY_ASSERT((value) >= 0); \
163 RARRAY(ary)->as.heap.aux.capa = (value); \
165#define FL_SET_SHARED_ROOT(ary) do { \
166 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
167 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
168 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
182ary_embed_capa(
VALUE ary)
184 size_t size = rb_gc_obj_slot_size(ary) - offsetof(
struct RArray, as.
ary);
186 return size /
sizeof(
VALUE);
190ary_embed_size(
long capa)
196ary_embeddable_p(
long capa)
198 return rb_gc_size_allocatable_p(ary_embed_size(
capa));
202rb_ary_embeddable_p(
VALUE ary)
212 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
216rb_ary_size_as_embedded(
VALUE ary)
220 if (ARY_EMBED_P(ary)) {
221 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
223 else if (rb_ary_embeddable_p(ary)) {
224 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
227 real_size =
sizeof(
struct RArray);
234#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
237ary_verify_(
VALUE ary,
const char *file,
int line)
241 if (ARY_SHARED_P(
ary)) {
250 else if (ARY_EMBED_P(
ary)) {
259 for (i=0; i<
len; i++) {
268#define ary_verify(ary) ((void)0)
297ary_mem_clear(
VALUE ary,
long beg,
long size)
305memfill(
register VALUE *mem,
register long size,
register VALUE val)
316 memfill(
ptr + beg, size, val);
326 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
327 rb_gc_writebarrier_remember(buff_owner_ary);
335 for (i=0; i<argc; i++) {
345 ary_memcpy0(
ary, beg, argc, argv,
ary);
349ary_heap_alloc_buffer(
size_t capa)
357 ruby_sized_xfree((
void *)
ptr, size);
363 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
367ary_heap_realloc(
VALUE ary,
size_t new_capa)
380 if (!ARY_EMBED_P(
ary)) {
381 const VALUE *buf = ARY_HEAP_PTR(
ary);
382 long len = ARY_HEAP_LEN(
ary);
385 ARY_SET_EMBED_LEN(
ary,
len);
394ary_resize_capa(
VALUE ary,
long capacity)
400 if (capacity > ary_embed_capa(
ary)) {
401 size_t new_capa = capacity;
402 if (ARY_EMBED_P(
ary)) {
403 long len = ARY_EMBED_LEN(
ary);
404 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
409 ARY_SET_HEAP_LEN(
ary,
len);
412 new_capa = ary_heap_realloc(
ary, capacity);
414 ARY_SET_CAPA(
ary, new_capa);
417 if (!ARY_EMBED_P(
ary)) {
418 long len = ARY_HEAP_LEN(
ary);
419 long old_capa = ARY_HEAP_CAPA(
ary);
422 if (
len > capacity)
len = capacity;
424 ary_heap_free_ptr(
ary,
ptr, old_capa);
437 long capacity = ARY_HEAP_LEN(
ary);
438 long old_capa = ARY_HEAP_CAPA(
ary);
441 if (old_capa > capacity) {
442 size_t new_capa = ary_heap_realloc(
ary, capacity);
443 ARY_SET_CAPA(
ary, new_capa);
452 long new_capa = ARY_CAPA(
ary) / 2;
454 if (new_capa < ARY_DEFAULT_SIZE) {
455 new_capa = ARY_DEFAULT_SIZE;
457 if (new_capa >= ARY_MAX_SIZE - min) {
458 new_capa = (ARY_MAX_SIZE - min) / 2;
461 ary_resize_capa(
ary, new_capa);
480 FL_UNSET_SHARED(
ary);
486 if (ARY_OWNS_HEAP_P(
ary)) {
489 else if (ARY_SHARED_P(
ary)) {
494 ARY_SET_EMBED_LEN(
ary, 0);
519 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
527 rb_check_frozen(
ary);
534 if (ARY_SHARED_P(
ary)) {
540 if (
len <= ary_embed_capa(
ary)) {
542 FL_UNSET_SHARED(
ary);
546 ARY_SET_EMBED_LEN(
ary,
len);
550 FL_UNSET_SHARED(
ary);
552 ARY_SET_CAPA(
ary, shared_len);
567 rb_gc_writebarrier_remember(
ary);
575 rb_ary_modify_check(
ary);
576 rb_ary_cancel_sharing(
ary);
580ary_ensure_room_for_push(
VALUE ary,
long add_len)
583 long new_len = old_len + add_len;
586 if (old_len > ARY_MAX_SIZE - add_len) {
589 if (ARY_SHARED_P(
ary)) {
590 if (new_len > ary_embed_capa(
ary)) {
594 rb_ary_modify_check(
ary);
605 ary_double_capa(
ary, new_len);
616 rb_ary_modify_check(
ary);
619 if (new_len >
capa) {
620 ary_double_capa(
ary, new_len);
651 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
652 ary_shrink_capa(
ary);
668 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
669 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
670 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
671 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
680 size_t size = ary_embed_size(
capa);
693ary_alloc_heap(
VALUE klass)
697 sizeof(struct
RArray), 0);
699 ary->as.heap.len = 0;
700 ary->as.heap.aux.capa = 0;
701 ary->as.heap.ptr = NULL;
707empty_ary_alloc(
VALUE klass)
709 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
710 return ary_alloc_embed(klass, 0);
721 rb_raise(rb_eArgError,
"negative array size (or size too big)");
723 if (
capa > ARY_MAX_SIZE) {
724 rb_raise(rb_eArgError,
"array size too big");
727 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
729 if (ary_embeddable_p(
capa)) {
730 ary = ary_alloc_embed(klass,
capa);
733 ary = ary_alloc_heap(klass);
737 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
738 ARY_SET_HEAP_LEN(
ary, 0);
757(rb_ary_new_from_args)(
long n, ...)
766 for (i=0; i<n; i++) {
776rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
780 ary = ary_new(klass, n);
782 ary_memcpy(
ary, 0, n, elts);
792 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
798 size_t size = ary_embed_size(
capa);
815 sizeof(struct
RArray), ec);
817 ary->as.heap.len = 0;
818 ary->as.heap.aux.capa = 0;
819 ary->as.heap.ptr = NULL;
830 rb_raise(rb_eArgError,
"negative array size (or size too big)");
832 if (
capa > ARY_MAX_SIZE) {
833 rb_raise(rb_eArgError,
"array size too big");
836 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
838 if (ary_embeddable_p(
capa)) {
839 ary = ec_ary_alloc_embed(ec, klass,
capa);
842 ary = ec_ary_alloc_heap(ec, klass);
846 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
847 ARY_SET_HEAP_LEN(
ary, 0);
860 ary_memcpy(
ary, 0, n, elts);
875rb_ary_hidden_new_fill(
long capa)
886 if (ARY_OWNS_HEAP_P(
ary)) {
887 if (USE_DEBUG_COUNTER &&
888 !ARY_SHARED_ROOT_P(
ary) &&
890 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
893 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
897 RB_DEBUG_COUNTER_INC(obj_ary_embed);
900 if (ARY_SHARED_P(
ary)) {
901 RB_DEBUG_COUNTER_INC(obj_ary_shared);
903 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
904 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
908static VALUE fake_ary_flags;
911init_fake_ary_flags(
void)
913 struct RArray fake_ary = {0};
921rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
924 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
930 return (
VALUE)fake_ary;
936 if (ARY_OWNS_HEAP_P(
ary)) {
937 return ARY_CAPA(
ary) *
sizeof(
VALUE);
949 if (ARY_SHARED_P(
ary)) {
950 return ARY_SHARED_ROOT(
ary);
952 else if (ARY_SHARED_ROOT_P(
ary)) {
964 VALUE shared = ary_alloc_heap(0);
965 FL_SET_SHARED_ROOT(shared);
967 if (ARY_EMBED_P(
ary)) {
969 ARY_SET_PTR(shared,
ptr);
973 ARY_SET_HEAP_LEN(
ary,
len);
980 ARY_SET_LEN(shared,
capa);
982 rb_ary_set_shared(
ary, shared);
996 if (ary_embeddable_p(
len)) {
1001 ARY_SET_EMBED_LEN(subst,
len);
1005 return rb_ary_increment_share(ary_make_shared(
ary));
1018 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1020#define to_ary rb_to_array_type
1025 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1031 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1037 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1066rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1072 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1074 if (size < 0) size = 0;
1077 ary = ary_new(klass, size);
1156 if (argc == 1 && !
FIXNUM_P(size)) {
1167 rb_raise(rb_eArgError,
"negative array size");
1169 if (
len > ARY_MAX_SIZE) {
1170 rb_raise(rb_eArgError,
"array size too big");
1174 ary_resize_capa(
ary,
len);
1179 rb_warn(
"block supersedes default value argument");
1181 for (i=0; i<
len; i++) {
1183 ARY_SET_LEN(
ary, i + 1);
1187 ary_memfill(
ary, 0,
len, val);
1204rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1207 if (argc > 0 && argv) {
1208 ary_memcpy(
ary, 0, argc, argv);
1209 ARY_SET_LEN(
ary, argc);
1223 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
1227 else if (idx >= ARY_MAX_SIZE) {
1232 if (idx >= ARY_CAPA(
ary)) {
1233 ary_double_capa(
ary, idx);
1240 ARY_SET_LEN(
ary, idx + 1);
1242 ARY_SET(
ary, idx, val);
1252 VALUE result = ary_alloc_heap(klass);
1253 size_t embed_capa = ary_embed_capa(result);
1254 if ((
size_t)
len <= embed_capa) {
1255 FL_SET_EMBED(result);
1257 ARY_SET_EMBED_LEN(result,
len);
1260 VALUE shared = ary_make_shared(
ary);
1265 FL_UNSET_EMBED(result);
1269 rb_ary_set_shared(result, shared);
1271 ARY_INCREASE_PTR(result, offset);
1272 ARY_SET_LEN(result,
len);
1282ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1289 const long orig_len =
len;
1291 if (step > 0 && step >=
len) {
1292 VALUE result = ary_new(klass, 1);
1297 ARY_SET_EMBED_LEN(result, 1);
1300 else if (step < 0 && step < -
len) {
1304 long ustep = (step < 0) ? -step : step;
1305 len = roomof(
len, ustep);
1308 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1310 VALUE result = ary_new(klass,
len);
1311 if (ARY_EMBED_P(result)) {
1315 for (i = 0; i <
len; ++i) {
1319 ARY_SET_EMBED_LEN(result,
len);
1325 for (i = 0; i <
len; ++i) {
1330 ARY_SET_LEN(result,
len);
1342enum ary_take_pos_flags
1349ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1358 rb_raise(rb_eArgError,
"negative array size");
1367ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1374 return ary_take_first_or_last_n(
ary,
NUM2LONG(argv[0]), last);
1396 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1400 ARY_SET_LEN(
ary, idx + 1);
1409 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1410 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1411 ARY_SET_LEN(
ary, oldlen +
len);
1443 rb_ary_modify_check(
ary);
1445 if (n == 0)
return Qnil;
1446 if (ARY_OWNS_HEAP_P(
ary) &&
1447 n * 3 < ARY_CAPA(
ary) &&
1448 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1450 ary_resize_capa(
ary, n * 2);
1455 ARY_SET_LEN(
ary, n - 1);
1499 rb_ary_modify_check(
ary);
1500 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1513 rb_ary_modify_check(
ary);
1519 rb_ary_behead(
ary, 1);
1572 rb_ary_modify_check(
ary);
1573 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1575 rb_ary_behead(
ary,n);
1587 rb_ary_modify_check(
ary);
1589 if (!ARY_SHARED_P(
ary)) {
1594 ARY_INCREASE_LEN(
ary, -n);
1599 ary_mem_clear(
ary, 0, n);
1600 ary_make_shared(
ary);
1602 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1603 ary_mem_clear(
ary, 0, n);
1606 ARY_INCREASE_PTR(
ary, n);
1607 ARY_INCREASE_LEN(
ary, -n);
1616 if (head - sharedp < argc) {
1617 long room =
capa -
len - argc;
1621 head = sharedp + argc + room;
1623 ARY_SET_PTR(
ary, head - argc);
1627 return ARY_SHARED_ROOT(
ary);
1631ary_modify_for_unshift(
VALUE ary,
int argc)
1634 long new_len =
len + argc;
1636 const VALUE *head, *sharedp;
1640 if (
capa - (
capa >> 6) <= new_len) {
1641 ary_double_capa(
ary, new_len);
1645 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1650 ary_make_shared(
ary);
1653 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1667ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1670 long new_len =
len + argc;
1672 if (
len > ARY_MAX_SIZE - argc) {
1675 else if (! ARY_SHARED_P(
ary)) {
1676 return ary_modify_for_unshift(
ary, argc);
1683 return ary_modify_for_unshift(
ary, argc);
1685 else if (new_len >
capa) {
1686 return ary_modify_for_unshift(
ary, argc);
1692 rb_ary_modify_check(
ary);
1693 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1719 rb_ary_modify_check(
ary);
1723 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1724 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1725 ARY_SET_LEN(
ary,
len + argc);
1732 return rb_ary_unshift_m(1, &item,
ary);
1741 if (offset < 0 ||
len <= offset) {
1750 return rb_ary_entry_internal(
ary, offset);
1754rb_ary_subseq_step(
VALUE ary,
long beg,
long len,
long step)
1759 if (beg > alen)
return Qnil;
1760 if (beg < 0 ||
len < 0)
return Qnil;
1762 if (alen <
len || alen < beg +
len) {
1766 if (
len == 0)
return ary_new(klass, 0);
1768 rb_raise(rb_eArgError,
"slice step cannot be zero");
1770 return ary_make_partial(
ary, klass, beg,
len);
1772 return ary_make_partial_step(
ary, klass, beg,
len, step);
1778 return rb_ary_subseq_step(
ary, beg,
len, 1);
1910 return rb_ary_aref2(
ary, argv[0], argv[1]);
1912 return rb_ary_aref1(
ary, argv[0]);
1929 long beg,
len, step;
1936 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1942 return rb_ary_subseq_step(
ary, beg,
len, step);
1987 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1993ary_first(
VALUE self)
2009 return ary_last(
ary);
2012 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2065 if (block_given && argc == 2) {
2066 rb_warn(
"block supersedes default value argument");
2074 if (block_given)
return rb_yield(pos);
2076 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2136 rb_warn(
"given block not used");
2194 rb_warn(
"given block not used");
2212 if (!
NIL_P(tmp))
return tmp;
2227 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2231 if (olen <
len || olen < beg +
len) {
2237 rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1;
2242 if (beg > ARY_MAX_SIZE - rlen) {
2245 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2247 ary_mem_clear(
ary, olen, beg - olen);
2250 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2257 if (olen -
len > ARY_MAX_SIZE - rlen) {
2261 alen = olen + rlen -
len;
2262 if (alen >= ARY_CAPA(
ary)) {
2263 ary_double_capa(
ary, alen);
2270 ARY_SET_LEN(
ary, alen);
2274 rb_gc_writebarrier_remember(
ary);
2296 rb_ary_modify_check(
ary);
2297 if (ARY_SHARED_P(
ary)) {
2301 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2313 if (
len == olen)
return ary;
2314 if (
len > ARY_MAX_SIZE) {
2318 if (
len > ARY_CAPA(
ary)) {
2319 ary_double_capa(
ary,
len);
2321 ary_mem_clear(
ary, olen,
len - olen);
2324 else if (ARY_EMBED_P(
ary)) {
2325 ARY_SET_EMBED_LEN(
ary,
len);
2327 else if (
len <= ary_embed_capa(
ary)) {
2329 long ptr_capa = ARY_HEAP_SIZE(
ary);
2330 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2335 ARY_SET_EMBED_LEN(
ary,
len);
2337 if (is_malloc_ptr) ruby_sized_xfree((
void *)
ptr, ptr_capa);
2340 if (olen >
len + ARY_DEFAULT_SIZE) {
2341 size_t new_capa = ary_heap_realloc(
ary,
len);
2342 ARY_SET_CAPA(
ary, new_capa);
2344 ARY_SET_HEAP_LEN(
ary,
len);
2513 long offset, beg,
len;
2516 rb_ary_modify_check(
ary);
2520 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2524 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2528 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2532 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2577 rb_ary_modify_check(
ary);
2579 if (argc == 1)
return ary;
2586 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2591 rb_ary_splice(
ary, pos, 0, argv + 1, argc - 1);
2601 return rb_ary_length(
ary);
2793 ARY_SET_LEN(dup,
len);
2811recursive_join(
VALUE obj,
VALUE argp,
int recur)
2816 VALUE result = arg[2];
2817 int *first = (
int *)arg[3];
2820 rb_raise(rb_eArgError,
"recursive array join");
2823 ary_join_1(obj,
ary, sep, 0, result, first);
2835 for (i=0; i<max; i++) {
2838 if (i > 0 && !
NIL_P(sep))
2846ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
2850 rb_enc_copy(dst, src);
2859 rb_raise(rb_eArgError,
"recursive array join");
2868 args[3] = (
VALUE)first;
2879 if (i > 0 && !
NIL_P(sep))
2884 ary_join_1_str(result, val, first);
2887 ary_join_1_ary(val,
ary, sep, result, val, first);
2890 ary_join_1_str(result, tmp, first);
2893 ary_join_1_ary(val,
ary, sep, result, tmp, first);
2905 VALUE val, tmp, result;
2917 if (
NIL_P(tmp) || tmp != val) {
2922 rb_enc_associate(result, rb_usascii_encoding());
2923 i = ary_join_0(
ary, sep, i, result);
2925 ary_join_1(
ary,
ary, sep, i, result, &first);
2929 len += RSTRING_LEN(tmp);
2995 else rb_enc_copy(str, s);
3026 return rb_ary_inspect(
ary);
3090 const VALUE e = rb_ary_elt(
ary, i);
3091 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3093 if (
NIL_P(key_value_pair)) {
3094 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3098 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3139 ary_reverse(p1, p2);
3185 do *p2-- = *p1++;
while (--
len > 0);
3192rotate_count(
long cnt,
long len)
3194 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3205 else if (cnt ==
len - 1) {
3213 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3225 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3317 cnt = rotate_count(cnt,
len);
3320 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3321 ary_memcpy(rotated,
len, cnt,
ptr);
3327struct ary_sort_data {
3333sort_reentered(
VALUE ary)
3335 if (
RBASIC(ary)->klass) {
3342sort_returned(
struct ary_sort_data *data)
3347 sort_reentered(data->ary);
3351sort_1(
const void *ap,
const void *bp,
void *dummy)
3353 struct ary_sort_data *data = dummy;
3354 VALUE retval = sort_reentered(data->ary);
3362 n = rb_cmpint(retval, a, b);
3363 sort_returned(data);
3368sort_2(
const void *ap,
const void *bp,
void *dummy)
3370 struct ary_sort_data *data = dummy;
3371 VALUE retval = sort_reentered(data->ary);
3376 if ((
long)a > (long)b)
return 1;
3377 if ((
long)a < (long)b)
return -1;
3380 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3384 return rb_float_cmp(a, b);
3387 retval = rb_funcallv(a, id_cmp, 1, &b);
3388 n = rb_cmpint(retval, a, b);
3389 sort_returned(data);
3410 VALUE tmp = ary_make_substitution(ary);
3411 struct ary_sort_data data;
3413 RBASIC_CLEAR_CLASS(tmp);
3415 data.receiver = ary;
3421 if (ARY_EMBED_P(tmp)) {
3422 if (ARY_SHARED_P(ary)) {
3423 rb_ary_unshare(ary);
3426 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3427 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3429 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3430 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3433 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3434 FL_UNSET_SHARED(ary);
3439 if (ARY_EMBED_P(ary)) {
3440 FL_UNSET_EMBED(ary);
3442 else if (ARY_SHARED_P(ary)) {
3444 rb_ary_unshare(ary);
3449 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3450 ARY_SET_HEAP_LEN(ary,
len);
3451 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3455 ARY_SET_EMBED_LEN(tmp, 0);
3523rb_ary_bsearch(
VALUE ary)
3525 VALUE index_result = rb_ary_bsearch_index(ary);
3530 return index_result;
3547rb_ary_bsearch_index(
VALUE ary)
3550 int smaller = 0, satisfied = 0;
3554 while (low < high) {
3555 mid = low + ((high - low) / 2);
3562 else if (v ==
Qtrue) {
3566 else if (!
RTEST(v)) {
3571 switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3573 case 1: smaller = 0;
break;
3574 case -1: smaller = 1;
3579 " (must be numeric, true, false or nil)",
3589 if (!satisfied)
return Qnil;
3623rb_ary_sort_by_bang(
VALUE ary)
3630 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3658rb_ary_collect(
VALUE ary)
3693rb_ary_collect_bang(
VALUE ary)
3709 long beg,
len, i, j;
3711 for (i=0; i<argc; i++) {
3718 long end = olen < beg+
len ? olen : beg+
len;
3719 for (j = beg; j < end; j++) {
3742 const long end = beg +
len;
3866rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
3870 for (i = 0; i < argc; ++i) {
3871 append_values_at_single(result, ary, olen, argv[i]);
3899rb_ary_select(
VALUE ary)
3914struct select_bang_arg {
3920select_bang_i(
VALUE a)
3922 volatile struct select_bang_arg *arg = (
void *)a;
3923 VALUE ary = arg->ary;
3926 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
3934 return (i1 == i2) ?
Qnil : ary;
3938select_bang_ensure(
VALUE a)
3940 volatile struct select_bang_arg *arg = (
void *)a;
3941 VALUE ary = arg->ary;
3943 long i1 = arg->len[0], i2 = arg->len[1];
3945 if (i2 <
len && i2 < i1) {
3954 ARY_SET_LEN(ary, i2 + tail);
3982rb_ary_select_bang(
VALUE ary)
3984 struct select_bang_arg args;
3990 args.len[0] = args.len[1] = 0;
4011rb_ary_keep_if(
VALUE ary)
4014 rb_ary_select_bang(ary);
4019ary_resize_smaller(
VALUE ary,
long len)
4023 ARY_SET_LEN(ary,
len);
4024 if (
len * 2 < ARY_CAPA(ary) &&
4025 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4026 ary_resize_capa(ary,
len * 2);
4074 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4093 ary_resize_smaller(ary, i2);
4104 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4119 ary_resize_smaller(ary, i2);
4131 if (pos < 0)
return Qnil;
4139 ARY_INCREASE_LEN(ary, -1);
4179ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4186 else if (pos < -orig_len) {
4192 else if (orig_len < pos) {
4195 if (orig_len < pos +
len) {
4196 len = orig_len - pos;
4203 rb_ary_splice(ary, pos,
len, 0, 0);
4301rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4306 rb_ary_modify_check(ary);
4313 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4320 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4349reject_bang_i(
VALUE a)
4351 volatile struct select_bang_arg *arg = (
void *)a;
4352 VALUE ary = arg->ary;
4355 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4363 return (i1 == i2) ?
Qnil : ary;
4367ary_reject_bang(
VALUE ary)
4369 struct select_bang_arg args;
4370 rb_ary_modify_check(ary);
4372 args.len[0] = args.len[1] = 0;
4397rb_ary_reject_bang(
VALUE ary)
4401 return ary_reject_bang(ary);
4422rb_ary_reject(
VALUE ary)
4428 ary_reject(ary, rejected_ary);
4429 return rejected_ary;
4450rb_ary_delete_if(
VALUE ary)
4454 ary_reject_bang(ary);
4469take_items(
VALUE obj,
long n)
4474 if (n == 0)
return result;
4477 args[0] = result; args[1] = (
VALUE)n;
4478 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4479 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4585 for (i=0; i<argc; i++) {
4586 argv[i] = take_items(argv[i],
len);
4590 int arity = rb_block_arity();
4599 for (j=0; j<argc; j++) {
4600 tmp[j+1] = rb_ary_elt(argv[j], i);
4612 for (j=0; j<argc; j++) {
4622 for (i=0; i<
len; i++) {
4626 for (j=0; j<argc; j++) {
4652rb_ary_transpose(
VALUE ary)
4654 long elen = -1, alen, i, j;
4655 VALUE tmp, result = 0;
4659 for (i=0; i<alen; i++) {
4660 tmp = to_ary(rb_ary_elt(ary, i));
4664 for (j=0; j<elen; j++) {
4669 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4672 for (j=0; j<elen; j++) {
4673 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4697 rb_ary_modify_check(copy);
4698 orig = to_ary(orig);
4699 if (copy == orig)
return copy;
4704 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4711 else if (ARY_EMBED_P(orig)) {
4712 long len = ARY_EMBED_LEN(orig);
4713 VALUE *ptr = ary_heap_alloc_buffer(
len);
4715 FL_UNSET_EMBED(copy);
4716 ARY_SET_PTR(copy, ptr);
4717 ARY_SET_LEN(copy,
len);
4718 ARY_SET_CAPA(copy,
len);
4727 VALUE shared_root = ary_make_shared(orig);
4728 FL_UNSET_EMBED(copy);
4729 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4730 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4731 rb_ary_set_shared(copy, shared_root);
4752 rb_ary_modify_check(ary);
4753 if (ARY_SHARED_P(ary)) {
4754 rb_ary_unshare(ary);
4756 ARY_SET_EMBED_LEN(ary, 0);
4759 ARY_SET_LEN(ary, 0);
4760 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4761 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
4952 long beg = 0, end = 0,
len = 0;
4975 if (beg < 0) beg = 0;
4984 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
4985 rb_raise(rb_eArgError,
"argument too big");
4989 if (end >= ARY_CAPA(ary)) {
4990 ary_resize_capa(ary, end);
4993 ARY_SET_LEN(ary, end);
4996 if (UNDEF_P(item)) {
5000 for (i=beg; i<end; i++) {
5007 ary_memfill(ary, beg,
len, item);
5029 long len, xlen, ylen;
5039 ARY_SET_LEN(z,
len);
5068rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5070 rb_ary_modify_check(ary);
5075 else if (argc > 1) {
5078 for (i = 0; i < argc; i++) {
5081 ary_append(ary, args);
5091 return ary_append(x, to_ary(y));
5130 rb_raise(rb_eArgError,
"negative argument");
5133 rb_raise(rb_eArgError,
"argument too big");
5138 ARY_SET_LEN(ary2,
len);
5143 ary_memcpy(ary2, 0, t, ptr);
5144 while (t <=
len/2) {
5221recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5224 const VALUE *p1, *p2;
5226 if (recur)
return Qtrue;
5233 for (i = 0; i < len1; i++) {
5281 if (ary1 == ary2)
return Qtrue;
5294recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5298 if (recur)
return Qtrue;
5300 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5328 if (ary1 == ary2)
return Qtrue;
5336ary_hash_values(
long len,
const VALUE *elements,
const VALUE ary)
5344 for (i=0; i<
len; i++) {
5345 n = rb_hash(elements[i]);
5357rb_ary_hash_values(
long len,
const VALUE *elements)
5359 return ary_hash_values(
len, elements, 0);
5378rb_ary_hash(
VALUE ary)
5429recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5433 if (recur)
return Qundef;
5438 for (i=0; i<
len; i++) {
5439 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5440 VALUE v = rb_funcallv(e1, id_cmp, 1, &e2);
5494 if (ary1 == ary2)
return INT2FIX(0);
5496 if (!UNDEF_P(v))
return v;
5510 rb_hash_add_new_element(hash, elt, elt);
5516ary_tmp_hash_new(
VALUE ary)
5519 VALUE hash = rb_hash_new_with_size(size);
5521 RBASIC_CLEAR_CLASS(hash);
5526ary_make_hash(
VALUE ary)
5528 VALUE hash = ary_tmp_hash_new(ary);
5529 return ary_add_hash(hash, ary);
5539 rb_hash_add_new_element(hash, k, v);
5545ary_make_hash_by(
VALUE ary)
5547 VALUE hash = ary_tmp_hash_new(ary);
5548 return ary_add_hash_by(hash, ary);
5576 ary2 = to_ary(ary2);
5577 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5582 VALUE elt = rb_ary_elt(ary1, i);
5583 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5589 hash = ary_make_hash(ary2);
5591 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5618rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5623 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5627 for (i = 0; i < argc; i++) {
5628 argv[i] = to_ary(argv[i]);
5629 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5630 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5635 VALUE elt = rb_ary_elt(ary, i);
5636 for (j = 0; j < argc; j++) {
5638 if (rb_hash_stlike_lookup(argv[j], elt, NULL))
5642 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5681 VALUE hash, ary3, v;
5685 ary2 = to_ary(ary2);
5692 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5693 if (rb_ary_includes_by_eql(ary3, v))
continue;
5699 hash = ary_make_hash(ary2);
5704 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5734rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5739 for (i = 0; i < argc; i++) {
5740 result = rb_ary_and(result, argv[i]);
5747ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5749 if (existing)
return ST_STOP;
5750 *key = *value = (
VALUE)arg;
5759 VALUE elt = rb_ary_elt(ary, i);
5760 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5771 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
5797 ary2 = to_ary(ary2);
5800 rb_ary_union(ary3, ary1);
5801 rb_ary_union(ary3, ary2);
5805 hash = ary_make_hash(ary1);
5806 rb_ary_union_hash(hash, ary2);
5808 return rb_hash_values(hash);
5835rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
5842 for (i = 0; i < argc; i++) {
5843 argv[i] = to_ary(argv[i]);
5847 if (sum <= SMALL_ARRAY_LEN) {
5850 rb_ary_union(ary_union, ary);
5851 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
5856 hash = ary_make_hash(ary);
5857 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
5859 return rb_hash_values(hash);
5879 VALUE hash, v, result, shorter, longer;
5883 ary2 = to_ary(ary2);
5889 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
5901 hash = ary_make_hash(shorter);
5907 if (rb_hash_stlike_lookup(hash, vv, 0)) {
5917ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
5925 if (rb_cmpint(rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
5934ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
5941 for (; i < n; ++i) {
5945 if ((
long)vmax < (
long)v) {
5950 return ary_max_generic(ary, i, vmax);
5958ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
5965 for (; i < n; ++i) {
5969 if (rb_float_cmp(vmax, v) < 0) {
5974 return ary_max_generic(ary, i, vmax);
5982ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
5989 for (; i < n; ++i) {
5998 return ary_max_generic(ary, i, vmax);
6061 return rb_nmin_run(ary, num, 0, 1, 1);
6067 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6075 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6076 return ary_max_opt_fixnum(ary, 1, result);
6078 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6079 return ary_max_opt_string(ary, 1, result);
6082 return ary_max_opt_float(ary, 1, result);
6085 return ary_max_generic(ary, 1, result);
6089 if (UNDEF_P(result))
return Qnil;
6094ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6102 if (rb_cmpint(rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6111ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6118 for (; i < n; ++i) {
6122 if ((
long)vmin > (
long)a) {
6127 return ary_min_generic(ary, i, vmin);
6135ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6142 for (; i < n; ++i) {
6146 if (rb_float_cmp(vmin, a) > 0) {
6151 return ary_min_generic(ary, i, vmin);
6159ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6166 for (; i < n; ++i) {
6175 return ary_min_generic(ary, i, vmin);
6238 return rb_nmin_run(ary, num, 0, 0, 1);
6244 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6252 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6253 return ary_min_opt_fixnum(ary, 1, result);
6255 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6256 return ary_min_opt_string(ary, 1, result);
6259 return ary_min_opt_float(ary, 1, result);
6262 return ary_min_generic(ary, 1, result);
6266 if (UNDEF_P(result))
return Qnil;
6293rb_ary_minmax(
VALUE ary)
6298 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6302push_value(st_data_t key, st_data_t val, st_data_t ary)
6336rb_ary_uniq_bang(
VALUE ary)
6341 rb_ary_modify_check(ary);
6345 hash = ary_make_hash_by(ary);
6347 hash = ary_make_hash(ary);
6353 rb_ary_modify_check(ary);
6354 ARY_SET_LEN(ary, 0);
6355 if (ARY_SHARED_P(ary)) {
6356 rb_ary_unshare(ary);
6359 ary_resize_capa(ary, hash_size);
6392rb_ary_uniq(
VALUE ary)
6401 hash = ary_make_hash_by(ary);
6402 uniq = rb_hash_values(hash);
6405 hash = ary_make_hash(ary);
6406 uniq = rb_hash_values(hash);
6429rb_ary_compact_bang(
VALUE ary)
6446 ary_resize_smaller(ary, n);
6466rb_ary_compact(
VALUE ary)
6469 rb_ary_compact_bang(ary);
6501rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6517 VALUE obj = argv[0];
6520 rb_warn(
"given block not used");
6531flatten(
VALUE ary,
int level)
6534 VALUE stack, result, tmp = 0, elt;
6550 ARY_SET_LEN(result, i);
6552 stack = ary_new(0, ARY_DEFAULT_SIZE);
6558 rb_hash_aset(memo, ary,
Qtrue);
6559 rb_hash_aset(memo, tmp,
Qtrue);
6568 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6573 if (
RBASIC(result)->klass) {
6575 rb_hash_clear(memo);
6584 if (rb_hash_aref(memo, tmp) ==
Qtrue) {
6585 rb_hash_clear(memo);
6586 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6588 rb_hash_aset(memo, tmp,
Qtrue);
6600 rb_hash_delete(memo, ary);
6608 rb_hash_clear(memo);
6651rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6653 int mod = 0, level = -1;
6657 rb_ary_modify_check(ary);
6659 if (level == 0)
return Qnil;
6661 result = flatten(ary, level);
6662 if (result == ary) {
6667 if (mod) ARY_SET_EMBED_LEN(result, 0);
6708rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6715 if (level == 0)
return ary_make_shared_copy(ary);
6718 result = flatten(ary, level);
6719 if (result == ary) {
6720 result = ary_make_shared_copy(ary);
6726#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6737 long j = RAND_UPTO(i);
6754 rb_ary_shuffle_bang(ec, ary, randgen);
6763 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
6770 long n,
len, i, j, k, idx[10];
6771 long rnds[numberof(idx)];
6772 long memo_threshold;
6781 return rb_ary_elt(ary, i);
6784 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
6786 if (n <= numberof(idx)) {
6787 for (i = 0; i < n; ++i) {
6788 rnds[i] = RAND_UPTO(
len - i);
6793 if (
len < k && n <= numberof(idx)) {
6794 for (i = 0; i < n; ++i) {
6804 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
6816 if (j >= i) l = i, g = ++j;
6817 if (k >= l && (++k >= g)) ++k;
6826 if (n <= numberof(idx)) {
6827 long sorted[numberof(idx)];
6828 sorted[0] = idx[0] = rnds[0];
6829 for (i=1; i<n; i++) {
6831 for (j = 0; j < i; ++j) {
6832 if (k < sorted[j])
break;
6835 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
6836 sorted[j] = idx[i] = k;
6840 for (i=0; i<n; i++) {
6845 else if (n <= memo_threshold / 2) {
6848 st_table *memo = st_init_numtable_with_size(n);
6852 for (i=0; i<n; i++) {
6853 long r = RAND_UPTO(
len-i) + i;
6855 if (r > max_idx) max_idx = r;
6858 if (
len <= max_idx) n = 0;
6859 else if (n >
len) n =
len;
6861 for (i=0; i<n; i++) {
6862 long j2 = j = ptr_result[i];
6865 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
6866 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
6867 st_insert(memo, (st_data_t)j, (st_data_t)i2);
6868 ptr_result[i] = ptr_ary[j2];
6873 st_free_table(memo);
6878 RBASIC_CLEAR_CLASS(result);
6881 for (i=0; i<n; i++) {
6882 j = RAND_UPTO(
len-i) + i;
6884 ptr_result[j] = ptr_result[i];
6888 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
6890 ARY_SET_LEN(result, n);
6918 if (mul <= 0)
return INT2FIX(0);
6920 return rb_fix_mul_fix(rb_ary_length(self), n);
6957rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
6964 if (argc == 0 ||
NIL_P(argv[0])) {
6969 if (n <= 0)
return Qnil;
6972 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
6986yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
6991 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
6992 ARY_SET_LEN(result, r);
6994 return !
RBASIC(values)->klass;
7010permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
7012 long i = 0, index = 0;
7015 const char *
const unused = memchr(&used[i], 0, n-i);
7030 for (i = 0; i < n; ++i) {
7031 if (used[i])
continue;
7033 if (!yield_indexed_values(values, r, p)) {
7049descending_factorial(
long from,
long how_many)
7054 while (--how_many > 0) {
7056 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7066binomial_coefficient(
long comb,
long size)
7070 if (comb > size-comb) {
7076 else if (comb == 0) {
7080 for (i = 1; i < comb; ++i) {
7081 r = rb_int_mul(r,
LONG2FIX(size - i));
7082 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7093 return descending_factorial(n, k);
7139rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7149 if (r < 0 || n < r) {
7162 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7163 char *used = (
char*)(p + r);
7164 VALUE ary0 = ary_make_shared_copy(ary);
7165 RBASIC_CLEAR_CLASS(ary0);
7169 permute0(n, r, p, used, ary0);
7177combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7184 for (lev++; lev < n; lev++) {
7185 stack[lev+1] = stack[lev]+1;
7187 if (!yield_indexed_values(values, n, stack+1)) {
7191 if (lev == 0)
return;
7193 }
while (stack[lev+1]+n ==
len+lev+1);
7203 return binomial_coefficient(k, n);
7258 if (n < 0 ||
len < n) {
7270 VALUE ary0 = ary_make_shared_copy(ary);
7272 long *stack =
ALLOCV_N(
long, t0, n+1);
7274 RBASIC_CLEAR_CLASS(ary0);
7275 combinate0(
len, n, stack, ary0);
7295rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7297 long i = 0, index = 0;
7301 if (++index < r-1) {
7305 for (i = 0; i < n; ++i) {
7307 if (!yield_indexed_values(values, r, p)) {
7312 if (index <= 0)
return;
7313 }
while ((i = ++p[--index]) >= n);
7371rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7393 VALUE ary0 = ary_make_shared_copy(ary);
7394 RBASIC_CLEAR_CLASS(ary0);
7396 rpermute0(n, r, p, ary0);
7404rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7406 long i = 0, index = 0;
7410 if (++index < r-1) {
7414 for (; i < n; ++i) {
7416 if (!yield_indexed_values(values, r, p)) {
7421 if (index <= 0)
return;
7422 }
while ((i = ++p[--index]) >= n);
7434 return binomial_coefficient(k, n + k - 1);
7477rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7495 else if (
len == 0) {
7501 VALUE ary0 = ary_make_shared_copy(ary);
7502 RBASIC_CLEAR_CLASS(ary0);
7504 rcombinate0(
len, n, p, n, ary0);
7565rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7571 int *counters =
ALLOCV_N(
int, t1, n);
7576 RBASIC_CLEAR_CLASS(t0);
7581 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7582 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7585 for (i = 0; i < n; i++) counters[i] = 0;
7590 for (i = 0; i < n; i++) {
7592 arrays[i] = ary_make_shared_copy(arrays[i]);
7597 for (i = 0; i < n; i++) {
7603 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7613 for (j = 0; j < n; j++) {
7618 if (
NIL_P(result)) {
7619 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7621 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7625 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7638 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7641 if (--m < 0)
goto done;
7649 return NIL_P(result) ? ary : result;
7675 rb_raise(rb_eArgError,
"attempt to take negative size");
7702rb_ary_take_while(
VALUE ary)
7710 return rb_ary_take(ary,
LONG2FIX(i));
7738 rb_raise(rb_eArgError,
"attempt to drop negative size");
7765rb_ary_drop_while(
VALUE ary)
7773 return rb_ary_drop(ary,
LONG2FIX(i));
7816rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
7824 rb_warn(
"given block not used");
7831 for (i = 0; i <
len; ++i) {
7883rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
7891 rb_warn(
"given block not used");
7898 for (i = 0; i <
len; ++i) {
7944rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
7952 rb_warn(
"given block not used");
7959 for (i = 0; i <
len; ++i) {
8008rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8017 rb_warn(
"given block not used");
8021 if (result)
return Qfalse;
8027 for (i = 0; i <
len; ++i) {
8029 if (result)
return Qfalse;
8037 if (result)
return Qfalse;
8069 self = rb_ary_at(self, *argv);
8070 if (!--argc)
return self;
8072 return rb_obj_dig(argc, argv, self,
Qnil);
8076finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8081 v = rb_rational_plus(r, v);
8149 goto init_is_a_value;
8163 else if (RB_BIGNUM_TYPE_P(e))
8164 v = rb_big_plus(e, v);
8169 r = rb_rational_plus(r, e);
8174 v = finish_exact_sum(n, r, v, argc!=0);
8178 v = finish_exact_sum(n, r, v, i!=0);
8190 goto has_float_value;
8200 else if (RB_BIGNUM_TYPE_P(e))
8207 if (isnan(f))
continue;
8213 if (isinf(f) && signbit(x) != signbit(f))
8219 if (isinf(f))
continue;
8222 if (fabs(f) >= fabs(x))
8235 goto has_some_value;
8249rb_ary_deconstruct(
VALUE ary)
8760 fake_ary_flags = init_fake_ary_flags();
8887 rb_vm_register_global_object(rb_cArray_empty_frozen);
8890#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_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.
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 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::@45 as
Array's specific fields.
struct RBasic basic
Basic part, including flags and class.
struct RArray::@45::@46 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.
long len
Number of elements of the array.
union RArray::@45::@46::@47 aux
Auxiliary info.
const VALUE * ptr
Pointer to the C array that holds the elements of the array.
VALUE flags
Per-object flags.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.