diff options
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 1261 |
1 files changed, 534 insertions, 727 deletions
@@ -28,7 +28,7 @@ #include "ruby/encoding.h" #include "ruby/st.h" #include "ruby/util.h" -#include "transient_heap.h" +#include "vm_core.h" #include "builtin.h" #if !ARRAY_DEBUG @@ -47,13 +47,8 @@ VALUE rb_cArray; * 2: RARRAY_SHARED_FLAG (equal to ELTS_SHARED) * The array is shared. The buffer this array points to is owned by * another array (the shared root). - * if USE_RVARGC * 3-9: RARRAY_EMBED_LEN * The length of the array when RARRAY_EMBED_FLAG is set. - * else - * 3-4: RARRAY_EMBED_LEN - * The length of the array when RARRAY_EMBED_FLAG is set. - * endif * 12: RARRAY_SHARED_ROOT_FLAG * The array is a shared root that does reference counting. The buffer * this array points to is owned by this array but may be pointed to @@ -63,8 +58,6 @@ VALUE rb_cArray; * they cannot be modified. Not updating the reference count * improves copy-on-write performance. Their reference count is * assumed to be infinity. - * 13: RARRAY_TRANSIENT_FLAG - * The buffer of the array is allocated on the transient heap. * 14: RARRAY_PTR_IN_USE_FLAG * The buffer of the array is in use. This is only used during * debugging. @@ -84,48 +77,47 @@ should_be_T_ARRAY(VALUE ary) return RB_TYPE_P(ary, T_ARRAY); } -#define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr) -#define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len) -#define ARY_HEAP_CAPA(a) (assert(!ARY_EMBED_P(a)), assert(!ARY_SHARED_ROOT_P(a)), \ +#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr) +#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len) +#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \ RARRAY(a)->as.heap.aux.capa) -#define ARY_EMBED_PTR(a) (assert(ARY_EMBED_P(a)), RARRAY(a)->as.ary) +#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary) #define ARY_EMBED_LEN(a) \ - (assert(ARY_EMBED_P(a)), \ + (RUBY_ASSERT(ARY_EMBED_P(a)), \ (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \ (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT))) -#define ARY_HEAP_SIZE(a) (assert(!ARY_EMBED_P(a)), assert(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE)) +#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE)) -#define ARY_OWNS_HEAP_P(a) (assert(should_be_T_ARRAY((VALUE)(a))), \ +#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \ !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG)) #define FL_SET_EMBED(a) do { \ - assert(!ARY_SHARED_P(a)); \ + RUBY_ASSERT(!ARY_SHARED_P(a)); \ FL_SET((a), RARRAY_EMBED_FLAG); \ - RARY_TRANSIENT_UNSET(a); \ ary_verify(a); \ } while (0) #define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK) #define FL_SET_SHARED(ary) do { \ - assert(!ARY_EMBED_P(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ FL_SET((ary), RARRAY_SHARED_FLAG); \ } while (0) #define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG) #define ARY_SET_PTR(ary, p) do { \ - assert(!ARY_EMBED_P(ary)); \ - assert(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ RARRAY(ary)->as.heap.ptr = (p); \ } while (0) #define ARY_SET_EMBED_LEN(ary, n) do { \ long tmp_n = (n); \ - assert(ARY_EMBED_P(ary)); \ + RUBY_ASSERT(ARY_EMBED_P(ary)); \ RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \ RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \ } while (0) #define ARY_SET_HEAP_LEN(ary, n) do { \ - assert(!ARY_EMBED_P(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ RARRAY(ary)->as.heap.len = (n); \ } while (0) #define ARY_SET_LEN(ary, n) do { \ @@ -135,15 +127,15 @@ should_be_T_ARRAY(VALUE ary) else { \ ARY_SET_HEAP_LEN((ary), (n)); \ } \ - assert(RARRAY_LEN(ary) == (n)); \ + RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \ } while (0) #define ARY_INCREASE_PTR(ary, n) do { \ - assert(!ARY_EMBED_P(ary)); \ - assert(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ RARRAY(ary)->as.heap.ptr += (n); \ } while (0) #define ARY_INCREASE_LEN(ary, n) do { \ - assert(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ if (ARY_EMBED_P(ary)) { \ ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \ } \ @@ -155,41 +147,30 @@ should_be_T_ARRAY(VALUE ary) #define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \ ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary)) #define ARY_SET_CAPA(ary, n) do { \ - assert(!ARY_EMBED_P(ary)); \ - assert(!ARY_SHARED_P(ary)); \ - assert(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ + RUBY_ASSERT(!ARY_SHARED_P(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ RARRAY(ary)->as.heap.aux.capa = (n); \ } while (0) -#define ARY_SET_SHARED(ary, value) do { \ - const VALUE _ary_ = (ary); \ - const VALUE _value_ = (value); \ - assert(!ARY_EMBED_P(_ary_)); \ - assert(ARY_SHARED_P(_ary_)); \ - assert(!OBJ_FROZEN(_ary_)); \ - assert(ARY_SHARED_ROOT_P(_value_) || OBJ_FROZEN(_value_)); \ - RB_OBJ_WRITE(_ary_, &RARRAY(_ary_)->as.heap.aux.shared_root, _value_); \ -} while (0) - #define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1) #define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \ - assert(ARY_SHARED_ROOT_P(ary)); \ - assert(!OBJ_FROZEN(ary)); \ - assert((value) >= 0); \ + RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT((value) >= 0); \ RARRAY(ary)->as.heap.aux.capa = (value); \ } while (0) #define FL_SET_SHARED_ROOT(ary) do { \ - assert(!OBJ_FROZEN(ary)); \ - assert(!ARY_EMBED_P(ary)); \ - assert(!RARRAY_TRANSIENT_P(ary)); \ + RUBY_ASSERT(!OBJ_FROZEN(ary)); \ + RUBY_ASSERT(!ARY_EMBED_P(ary)); \ FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \ } while (0) static inline void ARY_SET(VALUE a, long i, VALUE v) { - assert(!ARY_SHARED_P(a)); - assert(!OBJ_FROZEN(a)); + RUBY_ASSERT(!ARY_SHARED_P(a)); + RUBY_ASSERT(!OBJ_FROZEN(a)); RARRAY_ASET(a, i, v); } @@ -198,13 +179,9 @@ ARY_SET(VALUE a, long i, VALUE v) static long ary_embed_capa(VALUE ary) { -#if USE_RVARGC size_t size = rb_gc_obj_slot_size(ary) - offsetof(struct RArray, as.ary); - assert(size % sizeof(VALUE) == 0); + RUBY_ASSERT(size % sizeof(VALUE) == 0); return size / sizeof(VALUE); -#else - return RARRAY_EMBED_LEN_MAX; -#endif } static size_t @@ -216,18 +193,21 @@ ary_embed_size(long capa) static bool ary_embeddable_p(long capa) { -#if USE_RVARGC return rb_gc_size_allocatable_p(ary_embed_size(capa)); -#else - return capa <= RARRAY_EMBED_LEN_MAX; -#endif } bool rb_ary_embeddable_p(VALUE ary) { - // if the array is shared or a shared root then it's not moveable - return !(ARY_SHARED_P(ary) || ARY_SHARED_ROOT_P(ary)); + /* An array cannot be turned embeddable when the array is: + * - Shared root: other objects may point to the buffer of this array + * so we cannot make it embedded. + * - Frozen: this array may also be a shared root without the shared root + * flag. + * - Shared: we don't want to re-embed an array that points to a shared + * root (to save memory). + */ + return !(ARY_SHARED_ROOT_P(ary) || OBJ_FROZEN(ary) || ARY_SHARED_P(ary)); } size_t @@ -242,7 +222,7 @@ rb_ary_size_as_embedded(VALUE ary) real_size = ary_embed_size(ARY_HEAP_CAPA(ary)); } else { - real_size = sizeof(struct RString); + real_size = sizeof(struct RArray); } return real_size; } @@ -254,25 +234,23 @@ rb_ary_size_as_embedded(VALUE ary) static VALUE ary_verify_(VALUE ary, const char *file, int line) { - assert(RB_TYPE_P(ary, T_ARRAY)); + RUBY_ASSERT(RB_TYPE_P(ary, T_ARRAY)); if (ARY_SHARED_P(ary)) { VALUE root = ARY_SHARED_ROOT(ary); const VALUE *ptr = ARY_HEAP_PTR(ary); - const VALUE *root_ptr = RARRAY_CONST_PTR_TRANSIENT(root); + const VALUE *root_ptr = RARRAY_CONST_PTR(root); long len = ARY_HEAP_LEN(ary), root_len = RARRAY_LEN(root); - assert(ARY_SHARED_ROOT_P(root) || OBJ_FROZEN(root)); - assert(root_ptr <= ptr && ptr + len <= root_ptr + root_len); + RUBY_ASSERT(ARY_SHARED_ROOT_P(root) || OBJ_FROZEN(root)); + RUBY_ASSERT(root_ptr <= ptr && ptr + len <= root_ptr + root_len); ary_verify(root); } else if (ARY_EMBED_P(ary)) { - assert(!RARRAY_TRANSIENT_P(ary)); - assert(!ARY_SHARED_P(ary)); - assert(RARRAY_LEN(ary) <= ary_embed_capa(ary)); + RUBY_ASSERT(!ARY_SHARED_P(ary)); + RUBY_ASSERT(RARRAY_LEN(ary) <= ary_embed_capa(ary)); } else { -#if 1 - const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary); + const VALUE *ptr = RARRAY_CONST_PTR(ary); long i, len = RARRAY_LEN(ary); volatile VALUE v; if (len > 1) len = 1; /* check only HEAD */ @@ -280,17 +258,8 @@ ary_verify_(VALUE ary, const char *file, int line) v = ptr[i]; /* access check */ } v = v; -#endif } -#if USE_TRANSIENT_HEAP - if (RARRAY_TRANSIENT_P(ary)) { - assert(rb_transient_heap_managed_ptr_p(RARRAY_CONST_PTR_TRANSIENT(ary))); - } -#endif - - rb_transient_heap_verify(); - return ary; } @@ -309,7 +278,7 @@ rb_ary_ptr_use_start(VALUE ary) #if ARRAY_DEBUG FL_SET_RAW(ary, RARRAY_PTR_IN_USE_FLAG); #endif - return (VALUE *)RARRAY_CONST_PTR_TRANSIENT(ary); + return (VALUE *)RARRAY_CONST_PTR(ary); } void @@ -331,7 +300,7 @@ rb_mem_clear(VALUE *mem, long size) static void ary_mem_clear(VALUE ary, long beg, long size) { - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { rb_mem_clear(ptr + beg, size); }); } @@ -347,7 +316,7 @@ memfill(register VALUE *mem, register long size, register VALUE val) static void ary_memfill(VALUE ary, long beg, long size, VALUE val) { - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { memfill(ptr + beg, size, val); RB_OBJ_WRITTEN(ary, Qundef, val); }); @@ -356,17 +325,17 @@ ary_memfill(VALUE ary, long beg, long size, VALUE val) static void ary_memcpy0(VALUE ary, long beg, long argc, const VALUE *argv, VALUE buff_owner_ary) { - assert(!ARY_SHARED_P(buff_owner_ary)); + RUBY_ASSERT(!ARY_SHARED_P(buff_owner_ary)); if (argc > (int)(128/sizeof(VALUE)) /* is magic number (cache line size) */) { rb_gc_writebarrier_remember(buff_owner_ary); - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMCPY(ptr+beg, argv, VALUE, argc); }); } else { int i; - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { for (i=0; i<argc; i++) { RB_OBJ_WRITE(buff_owner_ary, &ptr[i+beg], argv[i]); } @@ -381,138 +350,45 @@ ary_memcpy(VALUE ary, long beg, long argc, const VALUE *argv) } static VALUE * -ary_heap_alloc(VALUE ary, size_t capa) +ary_heap_alloc(size_t capa) { - VALUE *ptr = rb_transient_heap_alloc(ary, sizeof(VALUE) * capa); - - if (ptr != NULL) { - RARY_TRANSIENT_SET(ary); - } - else { - RARY_TRANSIENT_UNSET(ary); - ptr = ALLOC_N(VALUE, capa); - } - - return ptr; + return ALLOC_N(VALUE, capa); } static void ary_heap_free_ptr(VALUE ary, const VALUE *ptr, long size) { - if (RARRAY_TRANSIENT_P(ary)) { - /* ignore it */ - } - else { - ruby_sized_xfree((void *)ptr, size); - } + ruby_sized_xfree((void *)ptr, size); } static void ary_heap_free(VALUE ary) { - if (RARRAY_TRANSIENT_P(ary)) { - RARY_TRANSIENT_UNSET(ary); - } - else { - ary_heap_free_ptr(ary, ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary)); - } + ary_heap_free_ptr(ary, ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary)); } static size_t ary_heap_realloc(VALUE ary, size_t new_capa) { - size_t alloc_capa = new_capa; - size_t old_capa = ARY_HEAP_CAPA(ary); - - if (RARRAY_TRANSIENT_P(ary)) { - if (new_capa <= old_capa) { - /* do nothing */ - alloc_capa = old_capa; - } - else { - VALUE *new_ptr = rb_transient_heap_alloc(ary, sizeof(VALUE) * new_capa); - - if (new_ptr == NULL) { - new_ptr = ALLOC_N(VALUE, new_capa); - RARY_TRANSIENT_UNSET(ary); - } - - MEMCPY(new_ptr, ARY_HEAP_PTR(ary), VALUE, old_capa); - ARY_SET_PTR(ary, new_ptr); - } - } - else { - SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, new_capa, old_capa); - } + SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, new_capa, ARY_HEAP_CAPA(ary)); ary_verify(ary); - return alloc_capa; -} - -#if USE_TRANSIENT_HEAP -static inline void -rb_ary_transient_heap_evacuate_(VALUE ary, int transient, int promote) -{ - if (transient) { - assert(!ARY_SHARED_ROOT_P(ary)); - - VALUE *new_ptr; - const VALUE *old_ptr = ARY_HEAP_PTR(ary); - long capa = ARY_HEAP_CAPA(ary); - - assert(ARY_OWNS_HEAP_P(ary)); - assert(RARRAY_TRANSIENT_P(ary)); - assert(!ARY_PTR_USING_P(ary)); - - if (promote) { - new_ptr = ALLOC_N(VALUE, capa); - RARY_TRANSIENT_UNSET(ary); - } - else { - new_ptr = ary_heap_alloc(ary, capa); - } - - MEMCPY(new_ptr, old_ptr, VALUE, capa); - /* do not use ARY_SET_PTR() because they assert !frozen */ - RARRAY(ary)->as.heap.ptr = new_ptr; - } - - ary_verify(ary); -} - -void -rb_ary_transient_heap_evacuate(VALUE ary, int promote) -{ - rb_ary_transient_heap_evacuate_(ary, RARRAY_TRANSIENT_P(ary), promote); + return new_capa; } void -rb_ary_detransient(VALUE ary) -{ - assert(RARRAY_TRANSIENT_P(ary)); - rb_ary_transient_heap_evacuate_(ary, TRUE, TRUE); -} -#else -void -rb_ary_detransient(VALUE ary) -{ - /* do nothing */ -} -#endif - -void rb_ary_make_embedded(VALUE ary) { - assert(rb_ary_embeddable_p(ary)); + RUBY_ASSERT(rb_ary_embeddable_p(ary)); if (!ARY_EMBED_P(ary)) { - VALUE *buf = RARRAY_PTR(ary); - long len = RARRAY_LEN(ary); + const VALUE *buf = ARY_HEAP_PTR(ary); + long len = ARY_HEAP_LEN(ary); FL_SET_EMBED(ary); ARY_SET_EMBED_LEN(ary, len); - RARY_TRANSIENT_UNSET(ary); - memmove(RARRAY_PTR(ary), buf, len * sizeof(VALUE)); + MEMCPY((void *)ARY_EMBED_PTR(ary), (void *)buf, VALUE, len); + ary_heap_free_ptr(ary, buf, len * sizeof(VALUE)); } } @@ -520,15 +396,15 @@ rb_ary_make_embedded(VALUE ary) static void ary_resize_capa(VALUE ary, long capacity) { - assert(RARRAY_LEN(ary) <= capacity); - assert(!OBJ_FROZEN(ary)); - assert(!ARY_SHARED_P(ary)); + RUBY_ASSERT(RARRAY_LEN(ary) <= capacity); + RUBY_ASSERT(!OBJ_FROZEN(ary)); + RUBY_ASSERT(!ARY_SHARED_P(ary)); if (capacity > ary_embed_capa(ary)) { size_t new_capa = capacity; if (ARY_EMBED_P(ary)) { long len = ARY_EMBED_LEN(ary); - VALUE *ptr = ary_heap_alloc(ary, capacity); + VALUE *ptr = ary_heap_alloc(capacity); MEMCPY(ptr, ARY_EMBED_PTR(ary), VALUE, len); FL_UNSET_EMBED(ary); @@ -563,8 +439,8 @@ ary_shrink_capa(VALUE ary) { long capacity = ARY_HEAP_LEN(ary); long old_capa = ARY_HEAP_CAPA(ary); - assert(!ARY_SHARED_P(ary)); - assert(old_capa >= capacity); + RUBY_ASSERT(!ARY_SHARED_P(ary)); + RUBY_ASSERT(old_capa >= capacity); if (old_capa > capacity) ary_heap_realloc(ary, capacity); ary_verify(ary); @@ -623,7 +499,7 @@ rb_ary_increment_share(VALUE shared_root) { if (!OBJ_FROZEN(shared_root)) { long num = ARY_SHARED_ROOT_REFCNT(shared_root); - assert(num >= 0); + RUBY_ASSERT(num >= 0); ARY_SET_SHARED_ROOT_REFCNT(shared_root, num + 1); } return shared_root; @@ -632,10 +508,15 @@ rb_ary_increment_share(VALUE shared_root) static void rb_ary_set_shared(VALUE ary, VALUE shared_root) { + RUBY_ASSERT(!ARY_EMBED_P(ary)); + RUBY_ASSERT(!OBJ_FROZEN(ary)); + RUBY_ASSERT(ARY_SHARED_ROOT_P(shared_root) || OBJ_FROZEN(shared_root)); + rb_ary_increment_share(shared_root); FL_SET_SHARED(ary); + RB_OBJ_WRITE(ary, &RARRAY(ary)->as.heap.aux.shared_root, shared_root); + RB_DEBUG_COUNTER_INC(obj_ary_shared_create); - ARY_SET_SHARED(ary, shared_root); } static inline void @@ -663,18 +544,18 @@ rb_ary_cancel_sharing(VALUE ary) ARY_SET_EMBED_LEN(ary, len); } else if (ARY_SHARED_ROOT_OCCUPIED(shared_root) && len > ((shared_len = RARRAY_LEN(shared_root))>>1)) { - long shift = RARRAY_CONST_PTR_TRANSIENT(ary) - RARRAY_CONST_PTR_TRANSIENT(shared_root); + long shift = RARRAY_CONST_PTR(ary) - RARRAY_CONST_PTR(shared_root); FL_UNSET_SHARED(ary); - ARY_SET_PTR(ary, RARRAY_CONST_PTR_TRANSIENT(shared_root)); + ARY_SET_PTR(ary, RARRAY_CONST_PTR(shared_root)); ARY_SET_CAPA(ary, shared_len); - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMMOVE(ptr, ptr+shift, VALUE, len); }); FL_SET_EMBED(shared_root); rb_ary_decrement_share(shared_root); } else { - VALUE *ptr = ary_heap_alloc(ary, len); + VALUE *ptr = ary_heap_alloc(len); MEMCPY(ptr, ARY_HEAP_PTR(ary), VALUE, len); rb_ary_unshare(ary); ARY_SET_CAPA(ary, len); @@ -707,7 +588,7 @@ ary_ensure_room_for_push(VALUE ary, long add_len) if (new_len > ary_embed_capa(ary)) { VALUE shared_root = ARY_SHARED_ROOT(ary); if (ARY_SHARED_ROOT_OCCUPIED(shared_root)) { - if (ARY_HEAP_PTR(ary) - RARRAY_CONST_PTR_TRANSIENT(shared_root) + new_len <= RARRAY_LEN(shared_root)) { + if (ARY_HEAP_PTR(ary) - RARRAY_CONST_PTR(shared_root) + new_len <= RARRAY_LEN(shared_root)) { rb_ary_modify_check(ary); ary_verify(ary); @@ -752,7 +633,7 @@ ary_ensure_room_for_push(VALUE ary, long add_len) * a.freeze * a.frozen? # => true * - * An attempt to modify a frozen \Array raises FrozenError. + * An attempt to modify a frozen +Array+ raises FrozenError. */ VALUE @@ -784,13 +665,10 @@ static VALUE ary_alloc_embed(VALUE klass, long capa) { size_t size = ary_embed_size(capa); - assert(rb_gc_size_allocatable_p(size)); -#if !USE_RVARGC - assert(size <= sizeof(struct RArray)); -#endif - RVARGC_NEWOBJ_OF(ary, struct RArray, klass, + RUBY_ASSERT(rb_gc_size_allocatable_p(size)); + NEWOBJ_OF(ary, struct RArray, klass, T_ARRAY | RARRAY_EMBED_FLAG | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), - size); + size, 0); /* Created array is: * FL_SET_EMBED((VALUE)ary); * ARY_SET_EMBED_LEN((VALUE)ary, 0); @@ -801,9 +679,9 @@ ary_alloc_embed(VALUE klass, long capa) static VALUE ary_alloc_heap(VALUE klass) { - RVARGC_NEWOBJ_OF(ary, struct RArray, klass, + NEWOBJ_OF(ary, struct RArray, klass, T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), - sizeof(struct RArray)); + sizeof(struct RArray), 0); return (VALUE)ary; } @@ -817,7 +695,7 @@ empty_ary_alloc(VALUE klass) static VALUE ary_new(VALUE klass, long capa) { - VALUE ary,*ptr; + VALUE ary; if (capa < 0) { rb_raise(rb_eArgError, "negative array size (or size too big)"); @@ -833,11 +711,10 @@ ary_new(VALUE klass, long capa) } else { ary = ary_alloc_heap(klass); - assert(!ARY_EMBED_P(ary)); - - ptr = ary_heap_alloc(ary, capa); - ARY_SET_PTR(ary, ptr); ARY_SET_CAPA(ary, capa); + RUBY_ASSERT(!ARY_EMBED_P(ary)); + + ARY_SET_PTR(ary, ary_heap_alloc(capa)); ARY_SET_HEAP_LEN(ary, 0); } @@ -875,7 +752,7 @@ VALUE return ary; } -MJIT_FUNC_EXPORTED VALUE +VALUE rb_ary_tmp_new_from_values(VALUE klass, long n, const VALUE *elts) { VALUE ary; @@ -899,13 +776,10 @@ static VALUE ec_ary_alloc_embed(rb_execution_context_t *ec, VALUE klass, long capa) { size_t size = ary_embed_size(capa); - assert(rb_gc_size_allocatable_p(size)); -#if !USE_RVARGC - assert(size <= sizeof(struct RArray)); -#endif - RB_RVARGC_EC_NEWOBJ_OF(ec, ary, struct RArray, klass, - T_ARRAY | RARRAY_EMBED_FLAG | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), - size); + RUBY_ASSERT(rb_gc_size_allocatable_p(size)); + NEWOBJ_OF(ary, struct RArray, klass, + T_ARRAY | RARRAY_EMBED_FLAG | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), + size, ec); /* Created array is: * FL_SET_EMBED((VALUE)ary); * ARY_SET_EMBED_LEN((VALUE)ary, 0); @@ -916,16 +790,16 @@ ec_ary_alloc_embed(rb_execution_context_t *ec, VALUE klass, long capa) static VALUE ec_ary_alloc_heap(rb_execution_context_t *ec, VALUE klass) { - RB_RVARGC_EC_NEWOBJ_OF(ec, ary, struct RArray, klass, - T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), - sizeof(struct RArray)); + NEWOBJ_OF(ary, struct RArray, klass, + T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0), + sizeof(struct RArray), ec); return (VALUE)ary; } static VALUE ec_ary_new(rb_execution_context_t *ec, VALUE klass, long capa) { - VALUE ary,*ptr; + VALUE ary; if (capa < 0) { rb_raise(rb_eArgError, "negative array size (or size too big)"); @@ -941,11 +815,10 @@ ec_ary_new(rb_execution_context_t *ec, VALUE klass, long capa) } else { ary = ec_ary_alloc_heap(ec, klass); - assert(!ARY_EMBED_P(ary)); - - ptr = ary_heap_alloc(ary, capa); - ARY_SET_PTR(ary, ptr); ARY_SET_CAPA(ary, capa); + RUBY_ASSERT(!ARY_EMBED_P(ary)); + + ARY_SET_PTR(ary, ary_heap_alloc(capa)); ARY_SET_HEAP_LEN(ary, 0); } @@ -970,7 +843,6 @@ VALUE rb_ary_hidden_new(long capa) { VALUE ary = ary_new(0, capa); - rb_ary_transient_heap_evacuate(ary, TRUE); return ary; } @@ -993,13 +865,8 @@ rb_ary_free(VALUE ary) RB_DEBUG_COUNTER_INC(obj_ary_extracapa); } - if (RARRAY_TRANSIENT_P(ary)) { - RB_DEBUG_COUNTER_INC(obj_ary_transient); - } - else { - RB_DEBUG_COUNTER_INC(obj_ary_ptr); - ary_heap_free(ary); - } + RB_DEBUG_COUNTER_INC(obj_ary_ptr); + ary_heap_free(ary); } else { RB_DEBUG_COUNTER_INC(obj_ary_embed); @@ -1013,7 +880,7 @@ rb_ary_free(VALUE ary) } } -RUBY_FUNC_EXPORTED size_t +size_t rb_ary_memsize(VALUE ary) { if (ARY_OWNS_HEAP_P(ary)) { @@ -1027,7 +894,6 @@ rb_ary_memsize(VALUE ary) static VALUE ary_make_shared(VALUE ary) { - assert(USE_RVARGC || !ARY_EMBED_P(ary)); ary_verify(ary); if (ARY_SHARED_P(ary)) { @@ -1038,43 +904,35 @@ ary_make_shared(VALUE ary) } else if (OBJ_FROZEN(ary)) { if (!ARY_EMBED_P(ary)) { - rb_ary_transient_heap_evacuate(ary, TRUE); ary_shrink_capa(ary); } return ary; } else { - rb_ary_transient_heap_evacuate(ary, TRUE); - long capa = ARY_CAPA(ary); long len = RARRAY_LEN(ary); /* Shared roots cannot be embedded because the reference count * (refcnt) is stored in as.heap.aux.capa. */ VALUE shared = ary_alloc_heap(0); + FL_SET_SHARED_ROOT(shared); if (ARY_EMBED_P(ary)) { - /* Cannot use ary_heap_alloc because we don't want to allocate - * on the transient heap. */ - VALUE *ptr = ALLOC_N(VALUE, capa); + VALUE *ptr = ary_heap_alloc(capa); ARY_SET_PTR(shared, ptr); - ary_memcpy(shared, 0, len, RARRAY_PTR(ary)); + ary_memcpy(shared, 0, len, RARRAY_CONST_PTR(ary)); FL_UNSET_EMBED(ary); ARY_SET_HEAP_LEN(ary, len); ARY_SET_PTR(ary, ptr); } else { - ARY_SET_PTR(shared, RARRAY_PTR(ary)); + ARY_SET_PTR(shared, RARRAY_CONST_PTR(ary)); } ARY_SET_LEN(shared, capa); ary_mem_clear(shared, len, capa - len); - FL_SET_SHARED_ROOT(shared); - ARY_SET_SHARED_ROOT_REFCNT(shared, 1); - FL_SET_SHARED(ary); - RB_DEBUG_COUNTER_INC(obj_ary_shared_create); - ARY_SET_SHARED(ary, shared); + rb_ary_set_shared(ary, shared); ary_verify(shared); ary_verify(ary); @@ -1090,9 +948,9 @@ ary_make_substitution(VALUE ary) if (ary_embeddable_p(len)) { VALUE subst = rb_ary_new_capa(len); - assert(ARY_EMBED_P(subst)); + RUBY_ASSERT(ARY_EMBED_P(subst)); - ary_memcpy(subst, 0, len, RARRAY_CONST_PTR_TRANSIENT(ary)); + ary_memcpy(subst, 0, len, RARRAY_CONST_PTR(ary)); ARY_SET_EMBED_LEN(subst, len); return subst; } @@ -1120,7 +978,7 @@ rb_check_array_type(VALUE ary) return rb_check_convert_type_with_id(ary, T_ARRAY, "Array", idTo_ary); } -MJIT_FUNC_EXPORTED VALUE +VALUE rb_check_to_array(VALUE ary) { return rb_check_convert_type_with_id(ary, T_ARRAY, "Array", idTo_a); @@ -1136,14 +994,14 @@ rb_to_array(VALUE ary) * call-seq: * Array.try_convert(object) -> object, new_array, or nil * - * If +object+ is an \Array object, returns +object+. + * If +object+ is an +Array+ object, returns +object+. * * Otherwise if +object+ responds to <tt>:to_ary</tt>, * calls <tt>object.to_ary</tt> and returns the result. * * Returns +nil+ if +object+ does not respond to <tt>:to_ary</tt> * - * Raises an exception unless <tt>object.to_ary</tt> returns an \Array object. + * Raises an exception unless <tt>object.to_ary</tt> returns an +Array+ object. */ static VALUE @@ -1184,33 +1042,33 @@ rb_ary_s_new(int argc, VALUE *argv, VALUE klass) * Array.new(size, default_value) -> new_array * Array.new(size) {|index| ... } -> new_array * - * Returns a new \Array. + * Returns a new +Array+. * - * With no block and no arguments, returns a new empty \Array object. + * With no block and no arguments, returns a new empty +Array+ object. * - * With no block and a single \Array argument +array+, - * returns a new \Array formed from +array+: + * With no block and a single +Array+ argument +array+, + * returns a new +Array+ formed from +array+: * * a = Array.new([:foo, 'bar', 2]) * a.class # => Array * a # => [:foo, "bar", 2] * - * With no block and a single \Integer argument +size+, - * returns a new \Array of the given size + * With no block and a single Integer argument +size+, + * returns a new +Array+ of the given size * whose elements are all +nil+: * * a = Array.new(3) * a # => [nil, nil, nil] * * With no block and arguments +size+ and +default_value+, - * returns an \Array of the given size; + * returns an +Array+ of the given size; * each element is that same +default_value+: * * a = Array.new(3, 'x') * a # => ['x', 'x', 'x'] * * With a block and argument +size+, - * returns an \Array of the given size; + * returns an +Array+ of the given size; * the block is called with each successive integer +index+; * the element for that +index+ is the return value from the block: * @@ -1221,7 +1079,7 @@ rb_ary_s_new(int argc, VALUE *argv, VALUE klass) * * With a block and no argument, * or a single argument +0+, - * ignores the block and returns a new empty \Array. + * ignores the block and returns a new empty +Array+. */ static VALUE @@ -1233,8 +1091,8 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary) rb_ary_modify(ary); if (argc == 0) { rb_ary_reset(ary); - assert(ARY_EMBED_P(ary)); - assert(ARY_EMBED_LEN(ary) == 0); + RUBY_ASSERT(ARY_EMBED_P(ary)); + RUBY_ASSERT(ARY_EMBED_LEN(ary) == 0); if (rb_block_given_p()) { rb_warning("given block not used"); } @@ -1331,25 +1189,26 @@ rb_ary_store(VALUE ary, long idx, VALUE val) static VALUE ary_make_partial(VALUE ary, VALUE klass, long offset, long len) { - assert(offset >= 0); - assert(len >= 0); - assert(offset+len <= RARRAY_LEN(ary)); - - const size_t rarray_embed_capa_max = (sizeof(struct RArray) - offsetof(struct RArray, as.ary)) / sizeof(VALUE); + RUBY_ASSERT(offset >= 0); + RUBY_ASSERT(len >= 0); + RUBY_ASSERT(offset+len <= RARRAY_LEN(ary)); - if ((size_t)len <= rarray_embed_capa_max && ary_embeddable_p(len)) { - VALUE result = ary_alloc_embed(klass, len); - ary_memcpy(result, 0, len, RARRAY_CONST_PTR_TRANSIENT(ary) + offset); + VALUE result = ary_alloc_heap(klass); + size_t embed_capa = ary_embed_capa(result); + if ((size_t)len <= embed_capa) { + FL_SET_EMBED(result); + ary_memcpy(result, 0, len, RARRAY_CONST_PTR(ary) + offset); ARY_SET_EMBED_LEN(result, len); - return result; } else { - VALUE result = ary_alloc_heap(klass); - assert(!ARY_EMBED_P(result)); - VALUE shared = ary_make_shared(ary); - ARY_SET_PTR(result, RARRAY_CONST_PTR_TRANSIENT(ary)); + /* The ary_make_shared call may allocate, which can trigger a GC + * compaction. This can cause the array to be embedded because it has + * a length of 0. */ + FL_UNSET_EMBED(result); + + ARY_SET_PTR(result, RARRAY_CONST_PTR(ary)); ARY_SET_LEN(result, RARRAY_LEN(ary)); rb_ary_set_shared(result, shared); @@ -1357,25 +1216,27 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len) ARY_SET_LEN(result, len); ary_verify(shared); - ary_verify(result); - return result; } + + ary_verify(result); + return result; } static VALUE ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step) { - assert(offset >= 0); - assert(len >= 0); - assert(offset+len <= RARRAY_LEN(ary)); - assert(step != 0); + RUBY_ASSERT(offset >= 0); + RUBY_ASSERT(len >= 0); + RUBY_ASSERT(offset+len <= RARRAY_LEN(ary)); + RUBY_ASSERT(step != 0); - const VALUE *values = RARRAY_CONST_PTR_TRANSIENT(ary); const long orig_len = len; if (step > 0 && step >= len) { VALUE result = ary_new(klass, 1); VALUE *ptr = (VALUE *)ARY_EMBED_PTR(result); + const VALUE *values = RARRAY_CONST_PTR(ary); + RB_OBJ_WRITE(result, ptr, values[offset]); ARY_SET_EMBED_LEN(result, 1); return result; @@ -1385,7 +1246,7 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step) } long ustep = (step < 0) ? -step : step; - len = (len + ustep - 1) / ustep; + len = roomof(len, ustep); long i; long j = offset + ((step > 0) ? 0 : (orig_len - 1)); @@ -1393,6 +1254,8 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step) VALUE result = ary_new(klass, len); if (ARY_EMBED_P(result)) { VALUE *ptr = (VALUE *)ARY_EMBED_PTR(result); + const VALUE *values = RARRAY_CONST_PTR(ary); + for (i = 0; i < len; ++i) { RB_OBJ_WRITE(result, ptr+i, values[j]); j += step; @@ -1400,7 +1263,9 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step) ARY_SET_EMBED_LEN(result, len); } else { - RARRAY_PTR_USE_TRANSIENT(result, ptr, { + const VALUE *values = RARRAY_CONST_PTR(ary); + + RARRAY_PTR_USE(result, ptr, { for (i = 0; i < len; ++i) { RB_OBJ_WRITE(result, ptr+i, values[j]); j += step; @@ -1425,20 +1290,11 @@ enum ary_take_pos_flags }; static VALUE -ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos_flags last) +ary_take_first_or_last_n(VALUE ary, long n, enum ary_take_pos_flags last) { - long n; - long len; + long len = RARRAY_LEN(ary); long offset = 0; - argc = rb_check_arity(argc, 0, 1); - /* the case optional argument is omitted should be handled in - * callers of this function. if another arity case is added, - * this arity check needs to rewrite. */ - RUBY_ASSERT_ALWAYS(argc == 1); - - n = NUM2LONG(argv[0]); - len = RARRAY_LEN(ary); if (n > len) { n = len; } @@ -1451,6 +1307,17 @@ ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos return ary_make_partial(ary, rb_cArray, offset, n); } +static VALUE +ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos_flags last) +{ + argc = rb_check_arity(argc, 0, 1); + /* the case optional argument is omitted should be handled in + * callers of this function. if another arity case is added, + * this arity check needs to rewrite. */ + RUBY_ASSERT_ALWAYS(argc == 1); + return ary_take_first_or_last_n(ary, NUM2LONG(argv[0]), last); +} + /* * call-seq: * array << object -> self @@ -1460,7 +1327,7 @@ ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos * a = [:foo, 'bar', 2] * a << :baz # => [:foo, "bar", 2, :baz] * - * Appends +object+ as one element, even if it is another \Array: + * Appends +object+ as one element, even if it is another +Array+: * * a = [:foo, 'bar', 2] * a1 = a << [3, 4] @@ -1473,7 +1340,7 @@ rb_ary_push(VALUE ary, VALUE item) { long idx = RARRAY_LEN((ary_verify(ary), ary)); VALUE target_ary = ary_ensure_room_for_push(ary, 1); - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { RB_OBJ_WRITE(target_ary, &ptr[idx], item); }); ARY_SET_LEN(ary, idx + 1); @@ -1502,14 +1369,12 @@ rb_ary_cat(VALUE ary, const VALUE *argv, long len) * a = [:foo, 'bar', 2] * a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat] * - * Appends each argument as one element, even if it is another \Array: + * Appends each argument as one element, even if it is another +Array+: * * a = [:foo, 'bar', 2] * a1 = a.push([:baz, :bat], [:bam, :bad]) * a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]] * - * Array#append is an alias for Array#push. - * * Related: #pop, #shift, #unshift. */ @@ -1554,9 +1419,9 @@ rb_ary_pop(VALUE ary) * * Returns +nil+ if the array is empty. * - * When a non-negative \Integer argument +n+ is given and is in range, + * When a non-negative Integer argument +n+ is given and is in range, * - * removes and returns the last +n+ elements in a new \Array: + * removes and returns the last +n+ elements in a new +Array+: * a = [:foo, 'bar', 2] * a.pop(2) # => ["bar", 2] * @@ -1618,20 +1483,20 @@ rb_ary_shift(VALUE ary) * * Returns +nil+ if +self+ is empty. * - * When positive \Integer argument +n+ is given, removes the first +n+ elements; - * returns those elements in a new \Array: + * When positive Integer argument +n+ is given, removes the first +n+ elements; + * returns those elements in a new +Array+: * * a = [:foo, 'bar', 2] * a.shift(2) # => [:foo, 'bar'] * a # => [2] * * If +n+ is as large as or larger than <tt>self.length</tt>, - * removes all elements; returns those elements in a new \Array: + * removes all elements; returns those elements in a new +Array+: * * a = [:foo, 'bar', 2] * a.shift(3) # => [:foo, 'bar', 2] * - * If +n+ is zero, returns a new empty \Array; +self+ is unmodified. + * If +n+ is zero, returns a new empty +Array+; +self+ is unmodified. * * Related: #push, #pop, #unshift. */ @@ -1654,7 +1519,7 @@ rb_ary_shift_m(int argc, VALUE *argv, VALUE ary) return result; } -MJIT_FUNC_EXPORTED VALUE +VALUE rb_ary_behead(VALUE ary, long n) { if (n <= 0) { @@ -1665,7 +1530,7 @@ rb_ary_behead(VALUE ary, long n) if (!ARY_SHARED_P(ary)) { if (ARY_EMBED_P(ary) || RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) { - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMMOVE(ptr, ptr + n, VALUE, RARRAY_LEN(ary) - n); }); /* WB: no new reference */ ARY_INCREASE_LEN(ary, -n); @@ -1698,7 +1563,7 @@ make_room_for_unshift(VALUE ary, const VALUE *head, VALUE *sharedp, int argc, lo head = sharedp + argc + room; } ARY_SET_PTR(ary, head - argc); - assert(ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary))); + RUBY_ASSERT(ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary))); ary_verify(ary); return ARY_SHARED_ROOT(ary); @@ -1726,12 +1591,12 @@ ary_modify_for_unshift(VALUE ary, int argc) capa = ARY_CAPA(ary); ary_make_shared(ary); - head = sharedp = RARRAY_CONST_PTR_TRANSIENT(ary); + head = sharedp = RARRAY_CONST_PTR(ary); return make_room_for_unshift(ary, head, (void *)sharedp, argc, capa, len); } else { /* sliding items */ - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMMOVE(ptr + argc, ptr, VALUE, len); }); @@ -1763,8 +1628,8 @@ ary_ensure_room_for_unshift(VALUE ary, int argc) return ary_modify_for_unshift(ary, argc); } else { - const VALUE * head = RARRAY_CONST_PTR_TRANSIENT(ary); - void *sharedp = (void *)RARRAY_CONST_PTR_TRANSIENT(shared_root); + const VALUE * head = RARRAY_CONST_PTR(ary); + void *sharedp = (void *)RARRAY_CONST_PTR(shared_root); rb_ary_modify_check(ary); return make_room_for_unshift(ary, head, sharedp, argc, capa, len); @@ -1781,12 +1646,10 @@ ary_ensure_room_for_unshift(VALUE ary, int argc) * a = [:foo, 'bar', 2] * a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2] * - * Array#prepend is an alias for Array#unshift. - * * Related: #push, #pop, #shift. */ -static VALUE +VALUE rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary) { long len = RARRAY_LEN(ary); @@ -1806,7 +1669,7 @@ rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary) VALUE rb_ary_unshift(VALUE ary, VALUE item) { - return rb_ary_unshift_m(1,&item,ary); + return rb_ary_unshift_m(1, &item, ary); } /* faster version - use this if you don't need to treat negative offset */ @@ -1870,7 +1733,17 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * * Returns elements from +self+; does not modify +self+. * - * When a single \Integer argument +index+ is given, returns the element at offset +index+: + * In brief: + * + * a = [:foo, 'bar', 2] + * a[0] # => :foo + * a[-1] # => 2 + * a[1, 2] # => ["bar", 2] + * a[0..1] # => [:foo, "bar"] + * a[0..-2] # => [:foo, "bar"] + * a[-2..2] # => ["bar", 2] + * + * When a single Integer argument +index+ is given, returns the element at offset +index+: * * a = [:foo, 'bar', 2] * a[0] # => :foo @@ -1885,8 +1758,8 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * * If +index+ is out of range, returns +nil+. * - * When two \Integer arguments +start+ and +length+ are given, - * returns a new \Array of size +length+ containing successive elements beginning at offset +start+: + * When two Integer arguments +start+ and +length+ are given, + * returns a new +Array+ of size +length+ containing successive elements beginning at offset +start+: * * a = [:foo, 'bar', 2] * a[0, 2] # => [:foo, "bar"] @@ -1901,11 +1774,11 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * a[2, 2] # => [2] * * If <tt>start == self.size</tt> and <tt>length >= 0</tt>, - * returns a new empty \Array. + * returns a new empty +Array+. * * If +length+ is negative, returns +nil+. * - * When a single \Range argument +range+ is given, + * When a single Range argument +range+ is given, * treats <tt>range.min</tt> as +start+ above * and <tt>range.size</tt> as +length+ above: * @@ -1913,7 +1786,7 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * a[0..1] # => [:foo, "bar"] * a[1..2] # => ["bar", 2] * - * Special case: If <tt>range.start == a.size</tt>, returns a new empty \Array. + * Special case: If <tt>range.start == a.size</tt>, returns a new empty +Array+. * * If <tt>range.end</tt> is negative, calculates the end index from the end: * @@ -1937,7 +1810,7 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * a[4..-1] # => nil * * When a single Enumerator::ArithmeticSequence argument +aseq+ is given, - * returns an \Array of elements corresponding to the indexes produced by + * returns an +Array+ of elements corresponding to the indexes produced by * the sequence. * * a = ['--', 'data1', '--', 'data2', '--', 'data3'] @@ -1959,7 +1832,6 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); * # Raises TypeError (no implicit conversion of Symbol into Integer): * a[:foo] * - * Array#slice is an alias for Array#[]. */ VALUE @@ -1983,7 +1855,7 @@ rb_ary_aref2(VALUE ary, VALUE b, VALUE e) return rb_ary_subseq(ary, beg, len); } -MJIT_FUNC_EXPORTED VALUE +VALUE rb_ary_aref1(VALUE ary, VALUE arg) { long beg, len, step; @@ -2009,7 +1881,7 @@ rb_ary_aref1(VALUE ary, VALUE arg) * call-seq: * array.at(index) -> object * - * Returns the element at \Integer offset +index+; does not modify +self+. + * Returns the element at Integer offset +index+; does not modify +self+. * a = [:foo, 'bar', 2] * a.at(0) # => :foo * a.at(2) # => 2 @@ -2022,39 +1894,7 @@ rb_ary_at(VALUE ary, VALUE pos) return rb_ary_entry(ary, NUM2LONG(pos)); } -/* - * call-seq: - * array.first -> object or nil - * array.first(n) -> new_array - * - * Returns elements from +self+; does not modify +self+. - * - * When no argument is given, returns the first element: - * - * a = [:foo, 'bar', 2] - * a.first # => :foo - * a # => [:foo, "bar", 2] - * - * If +self+ is empty, returns +nil+. - * - * When non-negative \Integer argument +n+ is given, - * returns the first +n+ elements in a new \Array: - * - * a = [:foo, 'bar', 2] - * a.first(2) # => [:foo, "bar"] - * - * If <tt>n >= array.size</tt>, returns all elements: - * - * a = [:foo, 'bar', 2] - * a.first(50) # => [:foo, "bar", 2] - * - * If <tt>n == 0</tt> returns an new empty \Array: - * - * a = [:foo, 'bar', 2] - * a.first(0) # [] - * - * Related: #last. - */ +#if 0 static VALUE rb_ary_first(int argc, VALUE *argv, VALUE ary) { @@ -2066,48 +1906,26 @@ rb_ary_first(int argc, VALUE *argv, VALUE ary) return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST); } } +#endif -/* - * call-seq: - * array.last -> object or nil - * array.last(n) -> new_array - * - * Returns elements from +self+; +self+ is not modified. - * - * When no argument is given, returns the last element: - * - * a = [:foo, 'bar', 2] - * a.last # => 2 - * a # => [:foo, "bar", 2] - * - * If +self+ is empty, returns +nil+. - * - * When non-negative \Innteger argument +n+ is given, - * returns the last +n+ elements in a new \Array: - * - * a = [:foo, 'bar', 2] - * a.last(2) # => ["bar", 2] - * - * If <tt>n >= array.size</tt>, returns all elements: - * - * a = [:foo, 'bar', 2] - * a.last(50) # => [:foo, "bar", 2] - * - * If <tt>n == 0</tt>, returns an new empty \Array: - * - * a = [:foo, 'bar', 2] - * a.last(0) # [] - * - * Related: #first. - */ +static VALUE +ary_first(VALUE self) +{ + return (RARRAY_LEN(self) == 0) ? Qnil : RARRAY_AREF(self, 0); +} + +static VALUE +ary_last(VALUE self) +{ + long len = RARRAY_LEN(self); + return (len == 0) ? Qnil : RARRAY_AREF(self, len-1); +} VALUE -rb_ary_last(int argc, const VALUE *argv, VALUE ary) +rb_ary_last(int argc, const VALUE *argv, VALUE ary) // used by parse.y { if (argc == 0) { - long len = RARRAY_LEN(ary); - if (len == 0) return Qnil; - return RARRAY_AREF(ary, len-1); + return ary_last(ary); } else { return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST); @@ -2122,7 +1940,7 @@ rb_ary_last(int argc, const VALUE *argv, VALUE ary) * * Returns the element at offset +index+. * - * With the single \Integer argument +index+, + * With the single Integer argument +index+, * returns the element at offset +index+: * * a = [:foo, 'bar', 2] @@ -2212,8 +2030,6 @@ rb_ary_fetch(int argc, VALUE *argv, VALUE ary) * e # => #<Enumerator: [:foo, "bar", 2]:index> * e.each {|element| element == 'bar' } # => 1 * - * Array#find_index is an alias for Array#index. - * * Related: #rindex. */ @@ -2268,7 +2084,7 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary) * * Returns +nil+ if the block never returns a truthy value. * - * When neither an argument nor a block is given, returns a new \Enumerator: + * When neither an argument nor a block is given, returns a new Enumerator: * * a = [:foo, 'bar', 2, 'bar'] * e = a.rindex @@ -2340,7 +2156,7 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen) } { - const VALUE *optr = RARRAY_CONST_PTR_TRANSIENT(ary); + const VALUE *optr = RARRAY_CONST_PTR(ary); rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1; } @@ -2353,7 +2169,7 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen) len = beg + rlen; ary_mem_clear(ary, olen, beg - olen); if (rlen > 0) { - if (rofs != -1) rptr = RARRAY_CONST_PTR_TRANSIENT(ary) + rofs; + if (rofs != -1) rptr = RARRAY_CONST_PTR(ary) + rofs; ary_memcpy0(ary, beg, rlen, rptr, target_ary); } ARY_SET_LEN(ary, len); @@ -2371,20 +2187,25 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen) } if (len != rlen) { - RARRAY_PTR_USE_TRANSIENT(ary, ptr, + RARRAY_PTR_USE(ary, ptr, MEMMOVE(ptr + beg + rlen, ptr + beg + len, VALUE, olen - (beg + len))); ARY_SET_LEN(ary, alen); } if (rlen > 0) { - if (rofs != -1) rptr = RARRAY_CONST_PTR_TRANSIENT(ary) + rofs; - /* give up wb-protected ary */ - RB_OBJ_WB_UNPROTECT_FOR(ARRAY, ary); + if (rofs == -1) { + rb_gc_writebarrier_remember(ary); + } + else { + /* In this case, we're copying from a region in this array, so + * we don't need to fire the write barrier. */ + rptr = RARRAY_CONST_PTR(ary) + rofs; + } /* do not use RARRAY_PTR() because it can causes GC. * ary can contain T_NONE object because it is not cleared. */ - RARRAY_PTR_USE_TRANSIENT(ary, ptr, + RARRAY_PTR_USE(ary, ptr, MEMMOVE(ptr + beg, rptr, VALUE, rlen)); } } @@ -2429,9 +2250,8 @@ rb_ary_resize(VALUE ary, long len) else if (len <= ary_embed_capa(ary)) { const VALUE *ptr = ARY_HEAP_PTR(ary); long ptr_capa = ARY_HEAP_SIZE(ary); - bool is_malloc_ptr = !ARY_SHARED_P(ary) && !RARRAY_TRANSIENT_P(ary); + bool is_malloc_ptr = !ARY_SHARED_P(ary); - FL_UNSET(ary, RARRAY_TRANSIENT_FLAG); FL_SET_EMBED(ary); MEMCPY((VALUE *)ARY_EMBED_PTR(ary), ptr, VALUE, len); /* WB: no new reference */ @@ -2461,7 +2281,7 @@ static VALUE ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val) { VALUE rpl = rb_ary_to_ary(val); - rb_ary_splice(ary, beg, len, RARRAY_CONST_PTR_TRANSIENT(rpl), RARRAY_LEN(rpl)); + rb_ary_splice(ary, beg, len, RARRAY_CONST_PTR(rpl), RARRAY_LEN(rpl)); RB_GC_GUARD(rpl); return val; } @@ -2474,7 +2294,32 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val) * * Assigns elements in +self+; returns the given +object+. * - * When \Integer argument +index+ is given, assigns +object+ to an element in +self+. + * In brief: + * + * a_orig = [:foo, 'bar', 2] + * # With argument index. + * a = a_orig.dup + * a[0] = 'foo' # => "foo" + * a # => ["foo", "bar", 2] + * a = a_orig.dup + * a[7] = 'foo' # => "foo" + * a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"] + * # With arguments start and length. + * a = a_orig.dup + * a[0, 2] = 'foo' # => "foo" + * a # => ["foo", 2] + * a = a_orig.dup + * a[6, 50] = 'foo' # => "foo" + * a # => [:foo, "bar", 2, nil, nil, nil, "foo"] + * # With argument range. + * a = a_orig.dup + * a[0..1] = 'foo' # => "foo" + * a # => ["foo", 2] + * a = a_orig.dup + * a[6..50] = 'foo' # => "foo" + * a # => [:foo, "bar", 2, nil, nil, nil, "foo"] + * + * When Integer argument +index+ is given, assigns +object+ to an element in +self+. * * If +index+ is non-negative, assigns +object+ the element at offset +index+: * @@ -2494,7 +2339,7 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val) * a[-1] = 'two' # => "two" * a # => [:foo, "bar", "two"] * - * When \Integer arguments +start+ and +length+ are given and +object+ is not an \Array, + * When Integer arguments +start+ and +length+ are given and +object+ is not an +Array+, * removes <tt>length - 1</tt> elements beginning at offset +start+, * and assigns +object+ at offset +start+: * @@ -2529,7 +2374,7 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val) * a[1, 5] = 'foo' # => "foo" * a # => [:foo, "foo"] * - * When \Range argument +range+ is given and +object+ is an \Array, + * When Range argument +range+ is given and +object+ is not an +Array+, * removes <tt>length - 1</tt> elements beginning at offset +start+, * and assigns +object+ at offset +start+: * @@ -2544,7 +2389,8 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val) * a # => [:foo, "foo"] * * If the array length is less than <tt>range.begin</tt>, - * assigns +object+ at offset <tt>range.begin</tt>, and ignores +length+: + * extends the array with +nil+, assigns +object+ at offset <tt>range.begin</tt>, + * and ignores +length+: * * a = [:foo, 'bar', 2] * a[6..50] = 'foo' # => "foo" @@ -2609,7 +2455,7 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary) * call-seq: * array.insert(index, *objects) -> self * - * Inserts given +objects+ before or after the element at \Integer index +offset+; + * Inserts given +objects+ before or after the element at Integer index +offset+; * returns +self+. * * When +index+ is non-negative, inserts all given +objects+ @@ -2674,50 +2520,19 @@ ary_enum_length(VALUE ary, VALUE args, VALUE eobj) return rb_ary_length(ary); } -/* - * call-seq: - * array.each {|element| ... } -> self - * array.each -> Enumerator - * - * Iterates over array elements. - * - * When a block given, passes each successive array element to the block; - * returns +self+: - * - * a = [:foo, 'bar', 2] - * a.each {|element| puts "#{element.class} #{element}" } - * - * Output: - * - * Symbol foo - * String bar - * Integer 2 - * - * Allows the array to be modified during iteration: - * - * a = [:foo, 'bar', 2] - * a.each {|element| puts element; a.clear if element.to_s.start_with?('b') } - * - * Output: - * - * foo - * bar - * - * When no block given, returns a new \Enumerator: - * a = [:foo, 'bar', 2] - * - * e = a.each - * e # => #<Enumerator: [:foo, "bar", 2]:each> - * a1 = e.each {|element| puts "#{element.class} #{element}" } - * - * Output: - * - * Symbol foo - * String bar - * Integer 2 - * - * Related: #each_index, #reverse_each. - */ +// Primitive to avoid a race condition in Array#each. +// Return `true` and write `value` and `index` if the element exists. +static VALUE +ary_fetch_next(VALUE self, VALUE *index, VALUE *value) +{ + long i = NUM2LONG(*index); + if (i >= RARRAY_LEN(self)) { + return Qfalse; + } + *value = RARRAY_AREF(self, i); + *index = LONG2NUM(i + 1); + return Qtrue; +} VALUE rb_ary_each(VALUE ary) @@ -2760,7 +2575,7 @@ rb_ary_each(VALUE ary) * 0 * 1 * - * When no block given, returns a new \Enumerator: + * When no block given, returns a new Enumerator: * * a = [:foo, 'bar', 2] * e = a.each_index @@ -2817,7 +2632,7 @@ rb_ary_each_index(VALUE ary) * 2 * bar * - * When no block given, returns a new \Enumerator: + * When no block given, returns a new Enumerator: * * a = [:foo, 'bar', 2] * e = a.reverse_each @@ -2884,7 +2699,7 @@ rb_ary_dup(VALUE ary) { long len = RARRAY_LEN(ary); VALUE dup = rb_ary_new2(len); - ary_memcpy(dup, 0, len, RARRAY_CONST_PTR_TRANSIENT(ary)); + ary_memcpy(dup, 0, len, RARRAY_CONST_PTR(ary)); ARY_SET_LEN(dup, len); ary_verify(ary); @@ -3037,7 +2852,7 @@ rb_ary_join(VALUE ary, VALUE sep) * array.join ->new_string * array.join(separator = $,) -> new_string * - * Returns the new \String formed by joining the array elements after conversion. + * Returns the new String formed by joining the array elements after conversion. * For each element +element+: * * - Uses <tt>element.to_s</tt> if +element+ is not a <tt>kind_of?(Array)</tt>. @@ -3097,13 +2912,12 @@ inspect_ary(VALUE ary, VALUE dummy, int recur) * call-seq: * array.inspect -> new_string * - * Returns the new \String formed by calling method <tt>#inspect</tt> + * Returns the new String formed by calling method <tt>#inspect</tt> * on each array element: * * a = [:foo, 'bar', 2] * a.inspect # => "[:foo, \"bar\", 2]" * - * Array#to_s is an alias for Array#inspect. */ static VALUE @@ -3123,12 +2937,12 @@ rb_ary_to_s(VALUE ary) * call-seq: * to_a -> self or new_array * - * When +self+ is an instance of \Array, returns +self+: + * When +self+ is an instance of +Array+, returns +self+: * * a = [:foo, 'bar', 2] * a.to_a # => [:foo, "bar", 2] * - * Otherwise, returns a new \Array containing the elements of +self+: + * Otherwise, returns a new +Array+ containing the elements of +self+: * * class MyArray < Array; end * a = MyArray.new(['foo', 'bar', 'two']) @@ -3156,18 +2970,18 @@ rb_ary_to_a(VALUE ary) * array.to_h -> new_hash * array.to_h {|item| ... } -> new_hash * - * Returns a new \Hash formed from +self+. + * Returns a new Hash formed from +self+. * * When a block is given, calls the block with each array element; - * the block must return a 2-element \Array whose two elements - * form a key-value pair in the returned \Hash: + * the block must return a 2-element +Array+ whose two elements + * form a key-value pair in the returned Hash: * * a = ['foo', :bar, 1, [2, 3], {baz: 4}] * h = a.to_h {|item| [item, item] } * h # => {"foo"=>"foo", :bar=>:bar, 1=>1, [2, 3]=>[2, 3], {:baz=>4}=>{:baz=>4}} * - * When no block is given, +self+ must be an \Array of 2-element sub-arrays, - * each sub-array is formed into a key-value pair in the new \Hash: + * When no block is given, +self+ must be an +Array+ of 2-element sub-arrays, + * each sub-array is formed into a key-value pair in the new Hash: * * [].to_h # => {} * a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']] @@ -3231,7 +3045,7 @@ rb_ary_reverse(VALUE ary) rb_ary_modify(ary); if (len > 1) { - RARRAY_PTR_USE_TRANSIENT(ary, p1, { + RARRAY_PTR_USE(ary, p1, { p2 = p1 + len - 1; /* points last item */ ary_reverse(p1, p2); }); /* WB: no new reference */ @@ -3260,7 +3074,7 @@ rb_ary_reverse_bang(VALUE ary) * call-seq: * array.reverse -> new_array * - * Returns a new \Array with the elements of +self+ in reverse order: + * Returns a new +Array+ with the elements of +self+ in reverse order: * * a = ['foo', 'bar', 'two'] * a1 = a.reverse @@ -3275,8 +3089,8 @@ rb_ary_reverse_m(VALUE ary) VALUE dup = rb_ary_new2(len); if (len > 0) { - const VALUE *p1 = RARRAY_CONST_PTR_TRANSIENT(ary); - VALUE *p2 = (VALUE *)RARRAY_CONST_PTR_TRANSIENT(dup) + len - 1; + const VALUE *p1 = RARRAY_CONST_PTR(ary); + VALUE *p2 = (VALUE *)RARRAY_CONST_PTR(dup) + len - 1; do *p2-- = *p1++; while (--len > 0); } ARY_SET_LEN(dup, RARRAY_LEN(ary)); @@ -3318,7 +3132,7 @@ rb_ary_rotate(VALUE ary, long cnt) if (cnt != 0) { long len = RARRAY_LEN(ary); if (len > 1 && (cnt = rotate_count(cnt, len)) > 0) { - RARRAY_PTR_USE_TRANSIENT(ary, ptr, ary_rotate_ptr(ptr, len, cnt)); + RARRAY_PTR_USE(ary, ptr, ary_rotate_ptr(ptr, len, cnt)); return ary; } } @@ -3337,7 +3151,7 @@ rb_ary_rotate(VALUE ary, long cnt) * a = [:foo, 'bar', 2, 'bar'] * a.rotate! # => ["bar", 2, "bar", :foo] * - * When given a non-negative \Integer +count+, + * When given a non-negative Integer +count+, * rotates +count+ elements from the beginning to the end: * * a = [:foo, 'bar', 2] @@ -3384,18 +3198,18 @@ rb_ary_rotate_bang(int argc, VALUE *argv, VALUE ary) * array.rotate -> new_array * array.rotate(count) -> new_array * - * Returns a new \Array formed from +self+ with elements + * Returns a new +Array+ formed from +self+ with elements * rotated from one end to the other. * - * When no argument given, returns a new \Array that is like +self+, + * When no argument given, returns a new +Array+ that is like +self+, * except that the first element has been rotated to the last position: * * a = [:foo, 'bar', 2, 'bar'] * a1 = a.rotate * a1 # => ["bar", 2, "bar", :foo] * - * When given a non-negative \Integer +count+, - * returns a new \Array with +count+ elements rotated from the beginning to the end: + * When given a non-negative Integer +count+, + * returns a new +Array+ with +count+ elements rotated from the beginning to the end: * * a = [:foo, 'bar', 2] * a1 = a.rotate(2) @@ -3413,7 +3227,7 @@ rb_ary_rotate_bang(int argc, VALUE *argv, VALUE ary) * a1 = a.rotate(0) * a1 # => [:foo, "bar", 2] * - * When given a negative \Integer +count+, rotates in the opposite direction, + * When given a negative Integer +count+, rotates in the opposite direction, * from end to beginning: * * a = [:foo, 'bar', 2] @@ -3440,7 +3254,7 @@ rb_ary_rotate_m(int argc, VALUE *argv, VALUE ary) rotated = rb_ary_new2(len); if (len > 0) { cnt = rotate_count(cnt, len); - ptr = RARRAY_CONST_PTR_TRANSIENT(ary); + ptr = RARRAY_CONST_PTR(ary); len -= cnt; ary_memcpy(rotated, 0, len, ptr + cnt); ary_memcpy(rotated, len, cnt, ptr); @@ -3452,7 +3266,6 @@ rb_ary_rotate_m(int argc, VALUE *argv, VALUE ary) struct ary_sort_data { VALUE ary; VALUE receiver; - struct cmp_opt_data cmp_opt; }; static VALUE @@ -3498,15 +3311,15 @@ sort_2(const void *ap, const void *bp, void *dummy) VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp; int n; - if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, Integer)) { + if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(INTEGER)) { if ((long)a > (long)b) return 1; if ((long)a < (long)b) return -1; return 0; } - if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, String)) { + if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) { return rb_str_cmp(a, b); } - if (RB_FLOAT_TYPE_P(a) && CMP_OPTIMIZABLE(data->cmp_opt, Float)) { + if (RB_FLOAT_TYPE_P(a) && CMP_OPTIMIZABLE(FLOAT)) { return rb_float_cmp(a, b); } @@ -3562,7 +3375,7 @@ VALUE rb_ary_sort_bang(VALUE ary) { rb_ary_modify(ary); - assert(!ARY_SHARED_P(ary)); + RUBY_ASSERT(!ARY_SHARED_P(ary)); if (RARRAY_LEN(ary) > 1) { VALUE tmp = ary_make_substitution(ary); /* only ary refers tmp */ struct ary_sort_data data; @@ -3570,8 +3383,6 @@ rb_ary_sort_bang(VALUE ary) RBASIC_CLEAR_CLASS(tmp); data.ary = tmp; data.receiver = ary; - data.cmp_opt.opt_methods = 0; - data.cmp_opt.opt_inited = 0; RARRAY_PTR_USE(tmp, ptr, { ruby_qsort(ptr, len, sizeof(VALUE), rb_block_given_p()?sort_1:sort_2, &data); @@ -3582,6 +3393,9 @@ rb_ary_sort_bang(VALUE ary) rb_ary_unshare(ary); FL_SET_EMBED(ary); } + if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) { + ary_resize_capa(ary, ARY_EMBED_LEN(tmp)); + } ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp)); ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp)); } @@ -3591,7 +3405,7 @@ rb_ary_sort_bang(VALUE ary) ARY_SET_CAPA(ary, RARRAY_LEN(tmp)); } else { - assert(!ARY_SHARED_P(tmp)); + RUBY_ASSERT(!ARY_SHARED_P(tmp)); if (ARY_EMBED_P(ary)) { FL_UNSET_EMBED(ary); } @@ -3624,7 +3438,7 @@ rb_ary_sort_bang(VALUE ary) * array.sort -> new_array * array.sort {|a, b| ... } -> new_array * - * Returns a new \Array whose elements are those from +self+, sorted. + * Returns a new +Array+ whose elements are those from +self+, sorted. * * With no block, compares elements using operator <tt><=></tt> * (see Comparable): @@ -3728,8 +3542,8 @@ rb_ary_bsearch_index(VALUE ary) const VALUE zero = INT2FIX(0); switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) { case 0: return INT2FIX(mid); - case 1: smaller = 1; break; - case -1: smaller = 0; + case 1: smaller = 0; break; + case -1: smaller = 1; } } else { @@ -3774,7 +3588,7 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, dummy)) * a.sort_by! {|element| element.size } * a # => ["d", "cc", "bbb", "aaaa"] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = ['aaaa', 'bbb', 'cc', 'd'] * a.sort_by! # => #<Enumerator: ["aaaa", "bbb", "cc", "d"]:sort_by!> @@ -3800,18 +3614,17 @@ rb_ary_sort_by_bang(VALUE ary) * array.map -> new_enumerator * * Calls the block, if given, with each element of +self+; - * returns a new \Array whose elements are the return values from the block: + * returns a new +Array+ whose elements are the return values from the block: * * a = [:foo, 'bar', 2] * a1 = a.map {|element| element.class } * a1 # => [Symbol, String, Integer] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * a = [:foo, 'bar', 2] * a1 = a.map * a1 # => #<Enumerator: [:foo, "bar", 2]:map> * - * Array#collect is an alias for Array#map. */ static VALUE @@ -3840,13 +3653,12 @@ rb_ary_collect(VALUE ary) * a = [:foo, 'bar', 2] * a.map! { |element| element.class } # => [Symbol, String, Integer] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2] * a1 = a.map! * a1 # => #<Enumerator: [:foo, "bar", 2]:map!> * - * Array#collect! is an alias for Array#map!. */ static VALUE @@ -3898,7 +3710,7 @@ append_values_at_single(VALUE result, VALUE ary, long olen, VALUE idx) /* check if idx is Range */ else if (rb_range_beg_len(idx, &beg, &len, olen, 1)) { if (len > 0) { - const VALUE *const src = RARRAY_CONST_PTR_TRANSIENT(ary); + const VALUE *const src = RARRAY_CONST_PTR(ary); const long end = beg + len; const long prevlen = RARRAY_LEN(result); if (beg < olen) { @@ -3920,8 +3732,8 @@ append_values_at_single(VALUE result, VALUE ary, long olen, VALUE idx) * call-seq: * array.values_at(*indexes) -> new_array * - * Returns a new \Array whose elements are the elements - * of +self+ at the given \Integer or \Range +indexes+. + * Returns a new +Array+ whose elements are the elements + * of +self+ at the given Integer or Range +indexes+. * * For each positive +index+, returns the element at offset +index+: * @@ -3940,7 +3752,7 @@ append_values_at_single(VALUE result, VALUE ary, long olen, VALUE idx) * a = [:foo, 'bar', 2] * a.values_at(0, 3, 1, 3) # => [:foo, nil, "bar", nil] * - * Returns a new empty \Array if no arguments given. + * Returns a new empty +Array+ if no arguments given. * * For each negative +index+, counts backward from the end of the array: * @@ -3978,19 +3790,18 @@ rb_ary_values_at(int argc, VALUE *argv, VALUE ary) * array.select -> new_enumerator * * Calls the block, if given, with each element of +self+; - * returns a new \Array containing those elements of +self+ + * returns a new +Array+ containing those elements of +self+ * for which the block returns a truthy value: * * a = [:foo, 'bar', 2, :bam] * a1 = a.select {|element| element.to_s.start_with?('b') } * a1 # => ["bar", :bam] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2, :bam] * a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select> * - * Array#filter is an alias for Array#select. */ static VALUE @@ -4045,7 +3856,7 @@ select_bang_ensure(VALUE a) rb_ary_modify(ary); if (i1 < len) { tail = len - i1; - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMMOVE(ptr + i2, ptr + i1, VALUE, tail); }); } @@ -4069,12 +3880,11 @@ select_bang_ensure(VALUE a) * * Returns +nil+ if no elements were removed. * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2, :bam] * a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!> * - * Array#filter! is an alias for Array#select!. */ static VALUE @@ -4101,7 +3911,7 @@ rb_ary_select_bang(VALUE ary) * a = [:foo, 'bar', 2, :bam] * a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2, :bam] * a.keep_if # => #<Enumerator: [:foo, "bar", 2, :bam]:keep_if> @@ -4233,7 +4043,7 @@ rb_ary_delete_at(VALUE ary, long pos) rb_ary_modify(ary); del = RARRAY_AREF(ary, pos); - RARRAY_PTR_USE_TRANSIENT(ary, ptr, { + RARRAY_PTR_USE(ary, ptr, { MEMMOVE(ptr+pos, ptr+pos+1, VALUE, len-pos-1); }); ARY_INCREASE_LEN(ary, -1); @@ -4245,7 +4055,7 @@ rb_ary_delete_at(VALUE ary, long pos) * call-seq: * array.delete_at(index) -> deleted_object or nil * - * Deletes an element from +self+, per the given \Integer +index+. + * Deletes an element from +self+, per the given Integer +index+. * * When +index+ is non-negative, deletes the element at offset +index+: * @@ -4294,7 +4104,7 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len) return rb_ary_new2(0); } else { - VALUE arg2 = rb_ary_new4(len, RARRAY_CONST_PTR_TRANSIENT(ary)+pos); + VALUE arg2 = rb_ary_new4(len, RARRAY_CONST_PTR(ary)+pos); rb_ary_splice(ary, pos, len, 0, 0); return arg2; } @@ -4308,7 +4118,7 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len) * * Removes and returns elements from +self+. * - * When the only argument is an \Integer +n+, + * When the only argument is an Integer +n+, * removes and returns the _nth_ element in +self+: * * a = [:foo, 'bar', 2] @@ -4325,7 +4135,7 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len) * * When the only arguments are Integers +start+ and +length+, * removes +length+ elements from +self+ beginning at offset +start+; - * returns the deleted objects in a new \Array: + * returns the deleted objects in a new +Array+: * * a = [:foo, 'bar', 2] * a.slice!(0, 2) # => [:foo, "bar"] @@ -4339,18 +4149,18 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len) * a # => [:foo] * * If <tt>start == a.size</tt> and +length+ is non-negative, - * returns a new empty \Array. + * returns a new empty +Array+. * * If +length+ is negative, returns +nil+. * - * When the only argument is a \Range object +range+, + * When the only argument is a Range object +range+, * treats <tt>range.min</tt> as +start+ above and <tt>range.size</tt> as +length+ above: * * a = [:foo, 'bar', 2] * a.slice!(1..2) # => ["bar", 2] * a # => [:foo] * - * If <tt>range.start == a.size</tt>, returns a new empty \Array. + * If <tt>range.start == a.size</tt>, returns a new empty +Array+. * * If <tt>range.start</tt> is larger than the array size, returns +nil+. * @@ -4459,7 +4269,7 @@ ary_reject_bang(VALUE ary) * * Returns +nil+ if no elements removed. * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2] * a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!> @@ -4479,14 +4289,14 @@ rb_ary_reject_bang(VALUE ary) * array.reject {|element| ... } -> new_array * array.reject -> new_enumerator * - * Returns a new \Array whose elements are all those from +self+ + * Returns a new +Array+ whose elements are all those from +self+ * for which the block returns +false+ or +nil+: * * a = [:foo, 'bar', 2, 'bat'] * a1 = a.reject {|element| element.to_s.start_with?('b') } * a1 # => [:foo, 2] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2] * a.reject # => #<Enumerator: [:foo, "bar", 2]:reject> @@ -4515,12 +4325,12 @@ rb_ary_reject(VALUE ary) * a = [:foo, 'bar', 2, 'bat'] * a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [:foo, 'bar', 2] * a.delete_if # => #<Enumerator: [:foo, "bar", 2]:delete_if> * -3 */ + */ static VALUE rb_ary_delete_if(VALUE ary) @@ -4551,7 +4361,7 @@ take_items(VALUE obj, long n) if (!NIL_P(result)) return rb_ary_subseq(result, 0, n); result = rb_ary_new2(n); args[0] = result; args[1] = (VALUE)n; - if (rb_check_block_call(obj, idEach, 0, 0, take_i, (VALUE)args) == Qundef) + if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (VALUE)args))) rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)", rb_obj_class(obj)); return result; @@ -4563,7 +4373,7 @@ take_items(VALUE obj, long n) * array.zip(*other_arrays) -> new_array * array.zip(*other_arrays) {|other_array| ... } -> nil * - * When no block given, returns a new \Array +new_array+ of size <tt>self.size</tt> + * When no block given, returns a new +Array+ +new_array+ of size <tt>self.size</tt> * whose elements are Arrays. * * Each nested array <tt>new_array[n]</tt> is of size <tt>other_arrays.size+1</tt>, @@ -4598,6 +4408,13 @@ take_items(VALUE obj, long n) * d = a.zip(b, c) * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]] * + * If an argument is not an array, it extracts the values by calling #each: + * + * a = [:a0, :a1, :a2, :a2] + * b = 1..4 + * c = a.zip(b) + * c # => [[:a0, 1], [:a1, 2], [:a2, 3], [:a2, 4]] + * * When a block is given, calls the block with each of the sub-arrays (formed as above); returns +nil+: * * a = [:a0, :a1, :a2, :a3] @@ -4676,7 +4493,7 @@ rb_ary_zip(int argc, VALUE *argv, VALUE ary) * call-seq: * array.transpose -> new_array * - * Transposes the rows and columns in an \Array of Arrays; + * Transposes the rows and columns in an +Array+ of Arrays; * the nested Arrays must all be the same size: * * a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]] @@ -4734,16 +4551,15 @@ rb_ary_replace(VALUE copy, VALUE orig) /* orig has enough space to embed the contents of orig. */ if (RARRAY_LEN(orig) <= ary_embed_capa(copy)) { - assert(ARY_EMBED_P(copy)); - ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR_TRANSIENT(orig)); + RUBY_ASSERT(ARY_EMBED_P(copy)); + ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR(orig)); ARY_SET_EMBED_LEN(copy, RARRAY_LEN(orig)); } -#if USE_RVARGC /* orig is embedded but copy does not have enough space to embed the * contents of orig. */ else if (ARY_EMBED_P(orig)) { long len = ARY_EMBED_LEN(orig); - VALUE *ptr = ary_heap_alloc(copy, len); + VALUE *ptr = ary_heap_alloc(len); FL_UNSET_EMBED(copy); ARY_SET_PTR(copy, ptr); @@ -4752,9 +4568,8 @@ rb_ary_replace(VALUE copy, VALUE orig) // No allocation and exception expected that could leave `copy` in a // bad state from the edits above. - ary_memcpy(copy, 0, len, RARRAY_CONST_PTR_TRANSIENT(orig)); + ary_memcpy(copy, 0, len, RARRAY_CONST_PTR(orig)); } -#endif /* Otherwise, orig is on heap and copy does not have enough space to embed * the contents of orig. */ else { @@ -4819,7 +4634,7 @@ rb_ary_clear(VALUE ary) * a # => ["a", "b", "c", "d"] * a.fill(:X) # => [:X, :X, :X, :X] * - * With arguments +obj+ and \Integer +start+, and no block given, + * With arguments +obj+ and Integer +start+, and no block given, * replaces elements based on the given start. * * If +start+ is in range (<tt>0 <= start < array.size</tt>), @@ -4847,7 +4662,7 @@ rb_ary_clear(VALUE ary) * a = ['a', 'b', 'c', 'd'] * a.fill(:X, -50) # => [:X, :X, :X, :X] * - * With arguments +obj+, \Integer +start+, and \Integer +length+, and no block given, + * With arguments +obj+, Integer +start+, and Integer +length+, and no block given, * replaces elements based on the given +start+ and +length+. * * If +start+ is in range, replaces +length+ elements beginning at offset +start+: @@ -4873,7 +4688,7 @@ rb_ary_clear(VALUE ary) * a.fill(:X, 1, 0) # => ["a", "b", "c", "d"] * a.fill(:X, 1, -1) # => ["a", "b", "c", "d"] * - * With arguments +obj+ and \Range +range+, and no block given, + * With arguments +obj+ and Range +range+, and no block given, * replaces elements based on the given range. * * If the range is positive and ascending (<tt>0 < range.begin <= range.end</tt>), @@ -5044,7 +4859,7 @@ rb_ary_fill(int argc, VALUE *argv, VALUE ary) ARY_SET_LEN(ary, end); } - if (item == Qundef) { + if (UNDEF_P(item)) { VALUE v; long i; @@ -5064,7 +4879,7 @@ rb_ary_fill(int argc, VALUE *argv, VALUE ary) * call-seq: * array + other_array -> new_array * - * Returns a new \Array containing all elements of +array+ + * Returns a new +Array+ containing all elements of +array+ * followed by all elements of +other_array+: * * a = [0, 1] + [2, 3] @@ -5085,8 +4900,8 @@ rb_ary_plus(VALUE x, VALUE y) len = xlen + ylen; z = rb_ary_new2(len); - ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x)); - ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y)); + ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR(x)); + ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR(y)); ARY_SET_LEN(z, len); return z; } @@ -5096,7 +4911,7 @@ ary_append(VALUE x, VALUE y) { long n = RARRAY_LEN(y); if (n > 0) { - rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR_TRANSIENT(y), n); + rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR(y), n); } RB_GC_GUARD(y); return x; @@ -5106,7 +4921,7 @@ ary_append(VALUE x, VALUE y) * call-seq: * array.concat(*other_arrays) -> self * - * Adds to +array+ all elements from each \Array in +other_arrays+; returns +self+: + * Adds to +array+ all elements from each +Array+ in +other_arrays+; returns +self+: * * a = [0, 1] * a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5] @@ -5144,13 +4959,13 @@ rb_ary_concat(VALUE x, VALUE y) * array * n -> new_array * array * string_separator -> new_string * - * When non-negative argument \Integer +n+ is given, - * returns a new \Array built by concatenating the +n+ copies of +self+: + * When non-negative argument Integer +n+ is given, + * returns a new +Array+ built by concatenating the +n+ copies of +self+: * * a = ['x', 'y'] * a * 3 # => ["x", "y", "x", "y", "x", "y"] * - * When \String argument +string_separator+ is given, + * When String argument +string_separator+ is given, * equivalent to <tt>array.join(string_separator)</tt>: * * [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {:foo=>0}" @@ -5185,16 +5000,16 @@ rb_ary_times(VALUE ary, VALUE times) ary2 = ary_new(rb_cArray, len); ARY_SET_LEN(ary2, len); - ptr = RARRAY_CONST_PTR_TRANSIENT(ary); + ptr = RARRAY_CONST_PTR(ary); t = RARRAY_LEN(ary); if (0 < t) { ary_memcpy(ary2, 0, t, ptr); while (t <= len/2) { - ary_memcpy(ary2, t, t, RARRAY_CONST_PTR_TRANSIENT(ary2)); + ary_memcpy(ary2, t, t, RARRAY_CONST_PTR(ary2)); t *= 2; } if (t < len) { - ary_memcpy(ary2, t, len-t, RARRAY_CONST_PTR_TRANSIENT(ary2)); + ary_memcpy(ary2, t, len-t, RARRAY_CONST_PTR(ary2)); } } out: @@ -5205,7 +5020,7 @@ rb_ary_times(VALUE ary, VALUE times) * call-seq: * array.assoc(obj) -> found_array or nil * - * Returns the first element in +self+ that is an \Array + * Returns the first element in +self+ that is an +Array+ * whose first element <tt>==</tt> +obj+: * * a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] @@ -5235,7 +5050,7 @@ rb_ary_assoc(VALUE ary, VALUE key) * call-seq: * array.rassoc(obj) -> found_array or nil * - * Returns the first element in +self+ that is an \Array + * Returns the first element in +self+ that is an +Array+ * whose second element <tt>==</tt> +obj+: * * a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] @@ -5253,7 +5068,7 @@ rb_ary_rassoc(VALUE ary, VALUE value) VALUE v; for (i = 0; i < RARRAY_LEN(ary); ++i) { - v = RARRAY_AREF(ary, i); + v = rb_check_array_type(RARRAY_AREF(ary, i)); if (RB_TYPE_P(v, T_ARRAY) && RARRAY_LEN(v) > 1 && rb_equal(RARRAY_AREF(v, 1), value)) @@ -5325,7 +5140,7 @@ rb_ary_equal(VALUE ary1, VALUE ary2) return rb_equal(ary2, ary1); } if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse; - if (RARRAY_CONST_PTR_TRANSIENT(ary1) == RARRAY_CONST_PTR_TRANSIENT(ary2)) return Qtrue; + if (RARRAY_CONST_PTR(ary1) == RARRAY_CONST_PTR(ary2)) return Qtrue; return rb_exec_recursive_paired(recursive_equal, ary1, ary2, ary2); } @@ -5344,10 +5159,10 @@ recursive_eql(VALUE ary1, VALUE ary2, int recur) /* * call-seq: - * array.eql? other_array -> true or false + * array.eql?(other_array) -> true or false * * Returns +true+ if +self+ and +other_array+ are the same size, - * and if, for each index +i+ in +self+, <tt>self[i].eql? other_array[i]</tt>: + * and if, for each index +i+ in +self+, <tt>self[i].eql?(other_array[i])</tt>: * * a0 = [:foo, 'bar', 2] * a1 = [:foo, 'bar', 2] @@ -5365,17 +5180,34 @@ rb_ary_eql(VALUE ary1, VALUE ary2) if (ary1 == ary2) return Qtrue; if (!RB_TYPE_P(ary2, T_ARRAY)) return Qfalse; if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse; - if (RARRAY_CONST_PTR_TRANSIENT(ary1) == RARRAY_CONST_PTR_TRANSIENT(ary2)) return Qtrue; + if (RARRAY_CONST_PTR(ary1) == RARRAY_CONST_PTR(ary2)) return Qtrue; return rb_exec_recursive_paired(recursive_eql, ary1, ary2, ary2); } +VALUE +rb_ary_hash_values(long len, const VALUE *elements) +{ + long i; + st_index_t h; + VALUE n; + + h = rb_hash_start(len); + h = rb_hash_uint(h, (st_index_t)rb_ary_hash_values); + for (i=0; i<len; i++) { + n = rb_hash(elements[i]); + h = rb_hash_uint(h, NUM2LONG(n)); + } + h = rb_hash_end(h); + return ST2FIX(h); +} + /* * call-seq: * array.hash -> integer * * Returns the integer hash value for +self+. * - * Two arrays with the same content will have the same hash code (and will compare using eql?): + * Two arrays with the same content will have the same hash code (and will compare using #eql?): * * [0, 1, 2].hash == [0, 1, 2].hash # => true * [0, 1, 2].hash == [0, 1, 3].hash # => false @@ -5385,18 +5217,7 @@ rb_ary_eql(VALUE ary1, VALUE ary2) static VALUE rb_ary_hash(VALUE ary) { - long i; - st_index_t h; - VALUE n; - - h = rb_hash_start(RARRAY_LEN(ary)); - h = rb_hash_uint(h, (st_index_t)rb_ary_hash); - for (i=0; i<RARRAY_LEN(ary); i++) { - n = rb_hash(RARRAY_AREF(ary, i)); - h = rb_hash_uint(h, NUM2LONG(n)); - } - h = rb_hash_end(h); - return ST2FIX(h); + return rb_ary_hash_values(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary)); } /* @@ -5501,7 +5322,7 @@ rb_ary_cmp(VALUE ary1, VALUE ary2) if (NIL_P(ary2)) return Qnil; if (ary1 == ary2) return INT2FIX(0); v = rb_exec_recursive_paired(recursive_cmp, ary1, ary2, ary2); - if (v != Qundef) return v; + if (!UNDEF_P(v)) return v; len = RARRAY_LEN(ary1) - RARRAY_LEN(ary2); if (len == 0) return INT2FIX(0); if (len > 0) return INT2FIX(1); @@ -5556,23 +5377,12 @@ ary_make_hash_by(VALUE ary) return ary_add_hash_by(hash, ary); } -static inline void -ary_recycle_hash(VALUE hash) -{ - assert(RBASIC_CLASS(hash) == 0); - if (RHASH_ST_TABLE_P(hash)) { - st_table *tbl = RHASH_ST_TABLE(hash); - st_free_table(tbl); - RHASH_ST_CLEAR(hash); - } -} - /* * call-seq: * array - other_array -> new_array * - * Returns a new \Array containing only those elements from +array+ - * that are not found in \Array +other_array+; + * Returns a new +Array+ containing only those elements from +array+ + * that are not found in +Array+ +other_array+; * items are compared using <tt>eql?</tt>; * the order from +array+ is preserved: * @@ -5583,7 +5393,7 @@ ary_recycle_hash(VALUE hash) * Related: Array#difference. */ -static VALUE +VALUE rb_ary_diff(VALUE ary1, VALUE ary2) { VALUE ary3; @@ -5608,7 +5418,7 @@ rb_ary_diff(VALUE ary1, VALUE ary2) if (rb_hash_stlike_lookup(hash, RARRAY_AREF(ary1, i), NULL)) continue; rb_ary_push(ary3, rb_ary_elt(ary1, i)); } - ary_recycle_hash(hash); + return ary3; } @@ -5616,7 +5426,7 @@ rb_ary_diff(VALUE ary1, VALUE ary2) * call-seq: * array.difference(*other_arrays) -> new_array * - * Returns a new \Array containing only those elements from +self+ + * Returns a new +Array+ containing only those elements from +self+ * that are not found in any of the Arrays +other_arrays+; * items are compared using <tt>eql?</tt>; order from +self+ is preserved: * @@ -5670,8 +5480,9 @@ rb_ary_difference_multi(int argc, VALUE *argv, VALUE ary) * call-seq: * array & other_array -> new_array * - * Returns a new \Array containing each element found in both +array+ and \Array +other_array+; - * duplicates are omitted; items are compared using <tt>eql?</tt>: + * Returns a new +Array+ containing each element found in both +array+ and +Array+ +other_array+; + * duplicates are omitted; items are compared using <tt>eql?</tt> + * (items must also implement +hash+ correctly): * * [0, 1, 2, 3] & [1, 2] # => [1, 2] * [0, 1, 0, 1] & [0, 1] # => [0, 1] @@ -5714,7 +5525,6 @@ rb_ary_and(VALUE ary1, VALUE ary2) rb_ary_push(ary3, v); } } - ary_recycle_hash(hash); return ary3; } @@ -5723,9 +5533,10 @@ rb_ary_and(VALUE ary1, VALUE ary2) * call-seq: * array.intersection(*other_arrays) -> new_array * - * Returns a new \Array containing each element found both in +self+ + * Returns a new +Array+ containing each element found both in +self+ * and in all of the given Arrays +other_arrays+; - * duplicates are omitted; items are compared using <tt>eql?</tt>: + * duplicates are omitted; items are compared using <tt>eql?</tt> + * (items must also implement +hash+ correctly): * * [0, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1] * [0, 0, 1, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1] @@ -5787,7 +5598,7 @@ rb_ary_union_hash(VALUE hash, VALUE ary2) * call-seq: * array | other_array -> new_array * - * Returns the union of +array+ and \Array +other_array+; + * Returns the union of +array+ and +Array+ +other_array+; * duplicates are removed; order is preserved; * items are compared using <tt>eql?</tt>: * @@ -5801,11 +5612,11 @@ rb_ary_union_hash(VALUE hash, VALUE ary2) static VALUE rb_ary_or(VALUE ary1, VALUE ary2) { - VALUE hash, ary3; + VALUE hash; ary2 = to_ary(ary2); if (RARRAY_LEN(ary1) + RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) { - ary3 = rb_ary_new(); + VALUE ary3 = rb_ary_new(); rb_ary_union(ary3, ary1); rb_ary_union(ary3, ary2); return ary3; @@ -5814,16 +5625,14 @@ rb_ary_or(VALUE ary1, VALUE ary2) hash = ary_make_hash(ary1); rb_ary_union_hash(hash, ary2); - ary3 = rb_hash_values(hash); - ary_recycle_hash(hash); - return ary3; + return rb_hash_values(hash); } /* * call-seq: * array.union(*other_arrays) -> new_array * - * Returns a new \Array that is the union of +self+ and all given Arrays +other_arrays+; + * Returns a new +Array+ that is the union of +self+ and all given Arrays +other_arrays+; * duplicates are removed; order is preserved; items are compared using <tt>eql?</tt>: * * [0, 1, 2, 3].union([4, 5], [6, 7]) # => [0, 1, 2, 3, 4, 5, 6, 7] @@ -5840,7 +5649,7 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary) { int i; long sum; - VALUE hash, ary_union; + VALUE hash; sum = RARRAY_LEN(ary); for (i = 0; i < argc; i++) { @@ -5849,7 +5658,7 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary) } if (sum <= SMALL_ARRAY_LEN) { - ary_union = rb_ary_new(); + VALUE ary_union = rb_ary_new(); rb_ary_union(ary_union, ary); for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]); @@ -5860,9 +5669,7 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary) hash = ary_make_hash(ary); for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]); - ary_union = rb_hash_values(hash); - ary_recycle_hash(hash); - return ary_union; + return rb_hash_values(hash); } /* @@ -5878,6 +5685,8 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary) * a.intersect?(b) #=> true * a.intersect?(c) #=> false * + * +Array+ elements are compared using <tt>eql?</tt> + * (items must also implement +hash+ correctly). */ static VALUE @@ -5916,7 +5725,6 @@ rb_ary_intersect_p(VALUE ary1, VALUE ary2) break; } } - ary_recycle_hash(hash); return result; } @@ -6020,30 +5828,30 @@ ary_max_opt_string(VALUE ary, long i, VALUE vmax) * Returns one of the following: * * - The maximum-valued element from +self+. - * - A new \Array of maximum-valued elements selected from +self+. + * - A new +Array+ of maximum-valued elements selected from +self+. * * When no block is given, each element in +self+ must respond to method <tt><=></tt> - * with an \Integer. + * with an Integer. * * With no argument and no block, returns the element in +self+ * having the maximum value per method <tt><=></tt>: * * [0, 1, 2].max # => 2 * - * With an argument \Integer +n+ and no block, returns a new \Array with at most +n+ elements, + * With an argument Integer +n+ and no block, returns a new +Array+ with at most +n+ elements, * in descending order per method <tt><=></tt>: * * [0, 1, 2, 3].max(3) # => [3, 2, 1] * [0, 1, 2, 3].max(6) # => [3, 2, 1, 0] * - * When a block is given, the block must return an \Integer. + * When a block is given, the block must return an Integer. * * With a block and no argument, calls the block <tt>self.size-1</tt> times to compare elements; * returns the element having the maximum value per the block: * * ['0', '00', '000'].max {|a, b| a.size <=> b.size } # => "000" * - * With an argument +n+ and a block, returns a new \Array with at most +n+ elements, + * With an argument +n+ and a block, returns a new +Array+ with at most +n+ elements, * in descending order per the block: * * ['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"] @@ -6052,7 +5860,6 @@ ary_max_opt_string(VALUE ary, long i, VALUE vmax) static VALUE rb_ary_max(int argc, VALUE *argv, VALUE ary) { - struct cmp_opt_data cmp_opt = { 0, 0 }; VALUE result = Qundef, v; VALUE num; long i; @@ -6064,7 +5871,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary) if (rb_block_given_p()) { for (i = 0; i < RARRAY_LEN(ary); i++) { v = RARRAY_AREF(ary, i); - if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) > 0) { + if (UNDEF_P(result) || rb_cmpint(rb_yield_values(2, v, result), v, result) > 0) { result = v; } } @@ -6072,13 +5879,13 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary) else if (n > 0) { result = RARRAY_AREF(ary, 0); if (n > 1) { - if (FIXNUM_P(result) && CMP_OPTIMIZABLE(cmp_opt, Integer)) { + if (FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) { return ary_max_opt_fixnum(ary, 1, result); } - else if (STRING_P(result) && CMP_OPTIMIZABLE(cmp_opt, String)) { + else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) { return ary_max_opt_string(ary, 1, result); } - else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(cmp_opt, Float)) { + else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(FLOAT)) { return ary_max_opt_float(ary, 1, result); } else { @@ -6086,7 +5893,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary) } } } - if (result == Qundef) return Qnil; + if (UNDEF_P(result)) return Qnil; return result; } @@ -6189,17 +5996,17 @@ ary_min_opt_string(VALUE ary, long i, VALUE vmin) * Returns one of the following: * * - The minimum-valued element from +self+. - * - A new \Array of minimum-valued elements selected from +self+. + * - A new +Array+ of minimum-valued elements selected from +self+. * * When no block is given, each element in +self+ must respond to method <tt><=></tt> - * with an \Integer. + * with an Integer. * * With no argument and no block, returns the element in +self+ * having the minimum value per method <tt><=></tt>: * * [0, 1, 2].min # => 0 * - * With \Integer argument +n+ and no block, returns a new \Array with at most +n+ elements, + * With Integer argument +n+ and no block, returns a new +Array+ with at most +n+ elements, * in ascending order per method <tt><=></tt>: * * [0, 1, 2, 3].min(3) # => [0, 1, 2] @@ -6212,7 +6019,7 @@ ary_min_opt_string(VALUE ary, long i, VALUE vmin) * * ['0', '00', '000'].min { |a, b| a.size <=> b.size } # => "0" * - * With an argument +n+ and a block, returns a new \Array with at most +n+ elements, + * With an argument +n+ and a block, returns a new +Array+ with at most +n+ elements, * in ascending order per the block: * * ['0', '00', '000'].min(2) {|a, b| a.size <=> b.size } # => ["0", "00"] @@ -6221,7 +6028,6 @@ ary_min_opt_string(VALUE ary, long i, VALUE vmin) static VALUE rb_ary_min(int argc, VALUE *argv, VALUE ary) { - struct cmp_opt_data cmp_opt = { 0, 0 }; VALUE result = Qundef, v; VALUE num; long i; @@ -6233,7 +6039,7 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary) if (rb_block_given_p()) { for (i = 0; i < RARRAY_LEN(ary); i++) { v = RARRAY_AREF(ary, i); - if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) < 0) { + if (UNDEF_P(result) || rb_cmpint(rb_yield_values(2, v, result), v, result) < 0) { result = v; } } @@ -6241,13 +6047,13 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary) else if (n > 0) { result = RARRAY_AREF(ary, 0); if (n > 1) { - if (FIXNUM_P(result) && CMP_OPTIMIZABLE(cmp_opt, Integer)) { + if (FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) { return ary_min_opt_fixnum(ary, 1, result); } - else if (STRING_P(result) && CMP_OPTIMIZABLE(cmp_opt, String)) { + else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) { return ary_min_opt_string(ary, 1, result); } - else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(cmp_opt, Float)) { + else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(FLOAT)) { return ary_min_opt_float(ary, 1, result); } else { @@ -6255,7 +6061,7 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary) } } } - if (result == Qundef) return Qnil; + if (UNDEF_P(result)) return Qnil; return result; } @@ -6264,19 +6070,19 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary) * array.minmax -> [min_val, max_val] * array.minmax {|a, b| ... } -> [min_val, max_val] * - * Returns a new 2-element \Array containing the minimum and maximum values + * Returns a new 2-element +Array+ containing the minimum and maximum values * from +self+, either per method <tt><=></tt> or per a given block:. * * When no block is given, each element in +self+ must respond to method <tt><=></tt> - * with an \Integer; - * returns a new 2-element \Array containing the minimum and maximum values + * with an Integer; + * returns a new 2-element +Array+ containing the minimum and maximum values * from +self+, per method <tt><=></tt>: * * [0, 1, 2].minmax # => [0, 2] * - * When a block is given, the block must return an \Integer; + * When a block is given, the block must return an Integer; * the block is called <tt>self.size-1</tt> times to compare elements; - * returns a new 2-element \Array containing the minimum and maximum values + * returns a new 2-element +Array+ containing the minimum and maximum values * from +self+, per the block: * * ['0', '00', '000'].minmax {|a, b| a.size <=> b.size } # => ["0", "000"] @@ -6353,7 +6159,6 @@ rb_ary_uniq_bang(VALUE ary) } ary_resize_capa(ary, hash_size); rb_hash_foreach(hash, push_value, ary); - ary_recycle_hash(hash); return ary; } @@ -6363,7 +6168,7 @@ rb_ary_uniq_bang(VALUE ary) * array.uniq -> new_array * array.uniq {|element| ... } -> new_array * - * Returns a new \Array containing those elements from +self+ that are not duplicates, + * Returns a new +Array+ containing those elements from +self+ that are not duplicates, * the first occurrence always being retained. * * With no block given, identifies and omits duplicates using method <tt>eql?</tt> @@ -6398,9 +6203,6 @@ rb_ary_uniq(VALUE ary) hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } - if (hash) { - ary_recycle_hash(hash); - } return uniq; } @@ -6421,14 +6223,14 @@ rb_ary_compact_bang(VALUE ary) long n; rb_ary_modify(ary); - p = t = (VALUE *)RARRAY_CONST_PTR_TRANSIENT(ary); /* WB: no new reference */ + p = t = (VALUE *)RARRAY_CONST_PTR(ary); /* WB: no new reference */ end = p + RARRAY_LEN(ary); while (t < end) { if (NIL_P(*t)) t++; else *p++ = *t++; } - n = p - RARRAY_CONST_PTR_TRANSIENT(ary); + n = p - RARRAY_CONST_PTR(ary); if (RARRAY_LEN(ary) == n) { return Qnil; } @@ -6441,7 +6243,7 @@ rb_ary_compact_bang(VALUE ary) * call-seq: * array.compact -> new_array * - * Returns a new \Array containing all non-+nil+ elements from +self+: + * Returns a new +Array+ containing all non-+nil+ elements from +self+: * * a = [nil, 0, nil, 1, nil, 2, nil] * a.compact # => [0, 1, 2] @@ -6516,9 +6318,8 @@ static VALUE flatten(VALUE ary, int level) { long i; - VALUE stack, result, tmp = 0, elt, vmemo; - st_table *memo = 0; - st_data_t id; + VALUE stack, result, tmp = 0, elt; + VALUE memo = Qfalse; for (i = 0; i < RARRAY_LEN(ary); i++) { elt = RARRAY_AREF(ary, i); @@ -6532,7 +6333,7 @@ flatten(VALUE ary, int level) } result = ary_new(0, RARRAY_LEN(ary)); - ary_memcpy(result, 0, i, RARRAY_CONST_PTR_TRANSIENT(ary)); + ary_memcpy(result, 0, i, RARRAY_CONST_PTR(ary)); ARY_SET_LEN(result, i); stack = ary_new(0, ARY_DEFAULT_SIZE); @@ -6540,12 +6341,9 @@ flatten(VALUE ary, int level) rb_ary_push(stack, LONG2NUM(i + 1)); if (level < 0) { - vmemo = rb_hash_new(); - RBASIC_CLEAR_CLASS(vmemo); - memo = st_init_numtable(); - rb_hash_st_table_set(vmemo, memo); - st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue); - st_insert(memo, (st_data_t)tmp, (st_data_t)Qtrue); + memo = rb_obj_hide(rb_ident_hash_new()); + rb_hash_aset(memo, ary, Qtrue); + rb_hash_aset(memo, tmp, Qtrue); } ary = tmp; @@ -6560,9 +6358,8 @@ flatten(VALUE ary, int level) } tmp = rb_check_array_type(elt); if (RBASIC(result)->klass) { - if (memo) { - RB_GC_GUARD(vmemo); - st_clear(memo); + if (RTEST(memo)) { + rb_hash_clear(memo); } rb_raise(rb_eRuntimeError, "flatten reentered"); } @@ -6571,12 +6368,11 @@ flatten(VALUE ary, int level) } else { if (memo) { - id = (st_data_t)tmp; - if (st_is_member(memo, id)) { - st_clear(memo); + if (rb_hash_aref(memo, tmp) == Qtrue) { + rb_hash_clear(memo); rb_raise(rb_eArgError, "tried to flatten recursive array"); } - st_insert(memo, id, (st_data_t)Qtrue); + rb_hash_aset(memo, tmp, Qtrue); } rb_ary_push(stack, ary); rb_ary_push(stack, LONG2NUM(i)); @@ -6588,8 +6384,7 @@ flatten(VALUE ary, int level) break; } if (memo) { - id = (st_data_t)ary; - st_delete(memo, &id, 0); + rb_hash_delete(memo, ary); } tmp = rb_ary_pop(stack); i = NUM2LONG(tmp); @@ -6597,7 +6392,7 @@ flatten(VALUE ary, int level) } if (memo) { - st_clear(memo); + rb_hash_clear(memo); } RBASIC_SET_CLASS(result, rb_cArray); @@ -6609,10 +6404,10 @@ flatten(VALUE ary, int level) * array.flatten! -> self or nil * array.flatten!(level) -> self or nil * - * Replaces each nested \Array in +self+ with the elements from that \Array; + * Replaces each nested +Array+ in +self+ with the elements from that +Array+; * returns +self+ if any changes, +nil+ otherwise. * - * With non-negative \Integer argument +level+, flattens recursively through +level+ levels: + * With non-negative Integer argument +level+, flattens recursively through +level+ levels: * * a = [ 0, [ 1, [2, 3], 4 ], 5 ] * a.flatten!(1) # => [0, 1, [2, 3], 4, 5] @@ -6662,11 +6457,11 @@ rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary) * array.flatten -> new_array * array.flatten(level) -> new_array * - * Returns a new \Array that is a recursive flattening of +self+: + * Returns a new +Array+ that is a recursive flattening of +self+: * - Each non-Array element is unchanged. - * - Each \Array is replaced by its individual elements. + * - Each +Array+ is replaced by its individual elements. * - * With non-negative \Integer argument +level+, flattens recursively through +level+ levels: + * With non-negative Integer argument +level+, flattens recursively through +level+ levels: * * a = [ 0, [ 1, [2, 3], 4 ], 5 ] * a.flatten(0) # => [0, [1, [2, 3], 4], 5] @@ -6722,7 +6517,7 @@ rb_ary_shuffle_bang(rb_execution_context_t *ec, VALUE ary, VALUE randgen) while (i) { long j = RAND_UPTO(i); VALUE tmp; - if (len != RARRAY_LEN(ary) || ptr != RARRAY_CONST_PTR_TRANSIENT(ary)) { + if (len != RARRAY_LEN(ary) || ptr != RARRAY_CONST_PTR(ary)) { rb_raise(rb_eRuntimeError, "modified during shuffle"); } tmp = ptr[--i]; @@ -6814,7 +6609,7 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE sorted[j] = idx[i] = k; } result = rb_ary_new_capa(n); - RARRAY_PTR_USE_TRANSIENT(result, ptr_result, { + RARRAY_PTR_USE(result, ptr_result, { for (i=0; i<n; i++) { ptr_result[i] = RARRAY_AREF(ary, idx[i]); } @@ -6837,7 +6632,7 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE len = RARRAY_LEN(ary); if (len <= max_idx) n = 0; else if (n > len) n = len; - RARRAY_PTR_USE_TRANSIENT(ary, ptr_ary, { + RARRAY_PTR_USE(ary, ptr_ary, { for (i=0; i<n; i++) { long j2 = j = ptr_result[i]; long i2 = i; @@ -6851,6 +6646,7 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE }); DATA_PTR(vmemo) = 0; st_free_table(memo); + RB_GC_GUARD(vmemo); } else { result = rb_ary_dup(ary); @@ -6900,7 +6696,7 @@ rb_ary_cycle_size(VALUE self, VALUE args, VALUE eobj) * array.cycle -> new_enumerator * array.cycle(count) -> new_enumerator * - * When called with positive \Integer argument +count+ and a block, + * When called with positive Integer argument +count+ and a block, * calls the block with each element, then does so again, * until it has done so +count+ times; returns +nil+: * @@ -6919,7 +6715,7 @@ rb_ary_cycle_size(VALUE self, VALUE args, VALUE eobj) * [0, 1].cycle {|element| puts element } * [0, 1].cycle(nil) {|element| puts element } * - * When no block is given, returns a new \Enumerator: + * When no block is given, returns a new Enumerator: * * [0, 1].cycle(2) # => #<Enumerator: [0, 1]:cycle(2)> * [0, 1].cycle # => # => #<Enumerator: [0, 1]:cycle> @@ -7076,7 +6872,7 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj) * When invoked with a block, yield all permutations of elements of +self+; returns +self+. * The order of permutations is indeterminate. * - * When a block and an in-range positive \Integer argument +n+ (<tt>0 < n <= self.size</tt>) + * When a block and an in-range positive Integer argument +n+ (<tt>0 < n <= self.size</tt>) * are given, calls the block with all +n+-tuple permutations of +self+. * * Example: @@ -7107,7 +6903,7 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj) * [2, 0, 1] * [2, 1, 0] * - * When +n+ is zero, calls the block once with a new empty \Array: + * When +n+ is zero, calls the block once with a new empty +Array+: * * a = [0, 1, 2] * a.permutation(0) {|permutation| p permutation } @@ -7138,7 +6934,7 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj) * [2, 0, 1] * [2, 1, 0] * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [0, 1, 2] * a.permutation # => #<Enumerator: [0, 1, 2]:permutation> @@ -7222,7 +7018,7 @@ rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj) * Calls the block, if given, with combinations of elements of +self+; * returns +self+. The order of combinations is indeterminate. * - * When a block and an in-range positive \Integer argument +n+ (<tt>0 < n <= self.size</tt>) + * When a block and an in-range positive Integer argument +n+ (<tt>0 < n <= self.size</tt>) * are given, calls the block with all +n+-tuple combinations of +self+. * * Example: @@ -7245,7 +7041,7 @@ rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj) * * [0, 1, 2] * - * When +n+ is zero, calls the block once with a new empty \Array: + * When +n+ is zero, calls the block once with a new empty +Array+: * * a = [0, 1, 2] * a1 = a.combination(0) {|combination| p combination } @@ -7261,7 +7057,7 @@ rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj) * a.combination(-1) {|combination| fail 'Cannot happen' } * a.combination(4) {|combination| fail 'Cannot happen' } * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [0, 1, 2] * a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)> @@ -7356,10 +7152,10 @@ rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj) * array.repeated_permutation(n) -> new_enumerator * * Calls the block with each repeated permutation of length +n+ of the elements of +self+; - * each permutation is an \Array; + * each permutation is an +Array+; * returns +self+. The order of the permutations is indeterminate. * - * When a block and a positive \Integer argument +n+ are given, calls the block with each + * When a block and a positive Integer argument +n+ are given, calls the block with each * +n+-tuple repeated permutation of the elements of +self+. * The number of permutations is <tt>self.size**n</tt>. * @@ -7390,13 +7186,13 @@ rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj) * [2, 1] * [2, 2] * - * If +n+ is zero, calls the block once with an empty \Array. + * If +n+ is zero, calls the block once with an empty +Array+. * * If +n+ is negative, does not call the block: * * a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' } * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [0, 1, 2] * a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)> @@ -7488,10 +7284,10 @@ rb_ary_repeated_combination_size(VALUE ary, VALUE args, VALUE eobj) * array.repeated_combination(n) -> new_enumerator * * Calls the block with each repeated combination of length +n+ of the elements of +self+; - * each combination is an \Array; + * each combination is an +Array+; * returns +self+. The order of the combinations is indeterminate. * - * When a block and a positive \Integer argument +n+ are given, calls the block with each + * When a block and a positive Integer argument +n+ are given, calls the block with each * +n+-tuple repeated combination of the elements of +self+. * The number of combinations is <tt>(n+1)(n+2)/2</tt>. * @@ -7519,13 +7315,13 @@ rb_ary_repeated_combination_size(VALUE ary, VALUE args, VALUE eobj) * [1, 2] * [2, 2] * - * If +n+ is zero, calls the block once with an empty \Array. + * If +n+ is zero, calls the block once with an empty +Array+. * * If +n+ is negative, does not call the block: * * a.repeated_combination(-1) {|combination| fail 'Cannot happen' } * - * Returns a new \Enumerator if no block given: + * Returns a new Enumerator if no block given: * * a = [0, 1, 2] * a.repeated_combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)> @@ -7592,7 +7388,7 @@ rb_ary_repeated_combination(VALUE ary, VALUE num) * including both +self+ and +other_arrays+. * - The order of the returned combinations is indeterminate. * - * When no block is given, returns the combinations as an \Array of Arrays: + * When no block is given, returns the combinations as an +Array+ of Arrays: * * a = [0, 1, 2] * a1 = [3, 4] @@ -7604,14 +7400,14 @@ rb_ary_repeated_combination(VALUE ary, VALUE num) * p.size # => 12 # a.size * a1.size * a2.size * p # => [[0, 3, 5], [0, 3, 6], [0, 4, 5], [0, 4, 6], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]] * - * If any argument is an empty \Array, returns an empty \Array. + * If any argument is an empty +Array+, returns an empty +Array+. * - * If no argument is given, returns an \Array of 1-element Arrays, + * If no argument is given, returns an +Array+ of 1-element Arrays, * each containing an element of +self+: * * a.product # => [[0], [1], [2]] * - * When a block is given, yields each combination as an \Array; returns +self+: + * When a block is given, yields each combination as an +Array+; returns +self+: * * a.product(a1) {|combination| p combination } * @@ -7624,11 +7420,11 @@ rb_ary_repeated_combination(VALUE ary, VALUE num) * [2, 3] * [2, 4] * - * If any argument is an empty \Array, does not call the block: + * If any argument is an empty +Array+, does not call the block: * * a.product(a1, a2, []) {|combination| fail 'Cannot happen' } * - * If no argument is given, yields each element of +self+ as a 1-element \Array: + * If no argument is given, yields each element of +self+ as a 1-element +Array+: * * a.product {|combination| p combination } * @@ -7732,8 +7528,8 @@ done: * call-seq: * array.take(n) -> new_array * - * Returns a new \Array containing the first +n+ element of +self+, - * where +n+ is a non-negative \Integer; + * Returns a new +Array+ containing the first +n+ element of +self+, + * where +n+ is a non-negative Integer; * does not modify +self+. * * Examples: @@ -7761,19 +7557,19 @@ rb_ary_take(VALUE obj, VALUE n) * array.take_while {|element| ... } -> new_array * array.take_while -> new_enumerator * - * Returns a new \Array containing zero or more leading elements of +self+; + * Returns a new +Array+ containing zero or more leading elements of +self+; * does not modify +self+. * * With a block given, calls the block with each successive element of +self+; * stops if the block returns +false+ or +nil+; - * returns a new \Array containing those elements for which the block returned a truthy value: + * returns a new +Array+ containing those elements for which the block returned a truthy value: * * a = [0, 1, 2, 3, 4, 5] * a.take_while {|element| element < 3 } # => [0, 1, 2] * a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5] * a # => [0, 1, 2, 3, 4, 5] * - * With no block given, returns a new \Enumerator: + * With no block given, returns a new Enumerator: * * [0, 1].take_while # => #<Enumerator: [0, 1]:take_while> * @@ -7795,8 +7591,8 @@ rb_ary_take_while(VALUE ary) * call-seq: * array.drop(n) -> new_array * - * Returns a new \Array containing all but the first +n+ element of +self+, - * where +n+ is a non-negative \Integer; + * Returns a new +Array+ containing all but the first +n+ element of +self+, + * where +n+ is a non-negative Integer; * does not modify +self+. * * Examples: @@ -7827,17 +7623,17 @@ rb_ary_drop(VALUE ary, VALUE n) * array.drop_while {|element| ... } -> new_array * array.drop_while -> new_enumerator - * Returns a new \Array containing zero or more trailing elements of +self+; + * Returns a new +Array+ containing zero or more trailing elements of +self+; * does not modify +self+. * * With a block given, calls the block with each successive element of +self+; * stops if the block returns +false+ or +nil+; - * returns a new \Array _omitting_ those elements for which the block returned a truthy value: + * returns a new +Array+ _omitting_ those elements for which the block returned a truthy value: * * a = [0, 1, 2, 3, 4, 5] * a.drop_while {|element| element < 3 } # => [3, 4, 5] * - * With no block given, returns a new \Enumerator: + * With no block given, returns a new Enumerator: * * [0, 1].drop_while # => # => #<Enumerator: [0, 1]:drop_while> * @@ -7863,6 +7659,9 @@ rb_ary_drop_while(VALUE ary) * * Returns +true+ if any element of +self+ meets a given criterion. * + * If +self+ has no element, returns +false+ and argument or block + * are not used. + * * With no block given and no argument, returns +true+ if +self+ has any truthy element, * +false+ otherwise: * @@ -7924,6 +7723,9 @@ rb_ary_any_p(int argc, VALUE *argv, VALUE ary) * * Returns +true+ if all elements of +self+ meet a given criterion. * + * If +self+ has no element, returns +true+ and argument or block + * are not used. + * * With no block given and no argument, returns +true+ if +self+ contains only truthy elements, * +false+ otherwise: * @@ -8144,7 +7946,7 @@ finish_exact_sum(long n, VALUE r, VALUE v, int z) { if (n != 0) v = rb_fix_plus(LONG2FIX(n), v); - if (r != Qundef) { + if (!UNDEF_P(r)) { v = rb_rational_plus(r, v); } else if (!n && z) { @@ -8188,7 +7990,7 @@ finish_exact_sum(long n, VALUE r, VALUE v, int z) * Notes: * * - Array#join and Array#flatten may be faster than Array#sum - * for an \Array of Strings or an \Array of Arrays. + * for an +Array+ of Strings or an +Array+ of Arrays. * - Array#sum method may not respect method redefinition of "+" methods such as Integer#+. * */ @@ -8209,6 +8011,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) n = 0; r = Qundef; + + if (!FIXNUM_P(v) && !RB_BIGNUM_TYPE_P(v) && !RB_TYPE_P(v, T_RATIONAL)) { + i = 0; + goto init_is_a_value; + } + for (i = 0; i < RARRAY_LEN(ary); i++) { e = RARRAY_AREF(ary, i); if (block_given) @@ -8223,7 +8031,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) else if (RB_BIGNUM_TYPE_P(e)) v = rb_big_plus(e, v); else if (RB_TYPE_P(e, T_RATIONAL)) { - if (r == Qundef) + if (UNDEF_P(r)) r = e; else r = rb_rational_plus(r, e); @@ -8293,6 +8101,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) } goto has_some_value; + init_is_a_value: for (; i < RARRAY_LEN(ary); i++) { e = RARRAY_AREF(ary, i); if (block_given) @@ -8303,6 +8112,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) return v; } +/* :nodoc: */ static VALUE rb_ary_deconstruct(VALUE ary) { @@ -8310,13 +8120,13 @@ rb_ary_deconstruct(VALUE ary) } /* - * An \Array is an ordered, integer-indexed collection of objects, called _elements_. + * An +Array+ is an ordered, integer-indexed collection of objects, called _elements_. * Any object (even another array) may be an array element, * and an array can contain objects of different types. * - * == \Array Indexes + * == +Array+ Indexes * - * \Array indexing starts at 0, as in C or Java. + * +Array+ indexing starts at 0, as in C or Java. * * A positive index is an offset from the first element: * @@ -8343,14 +8153,14 @@ rb_ary_deconstruct(VALUE ary) * - Index -4 is out of range. * * Although the effective index into an array is always an integer, - * some methods (both within and outside of class \Array) + * some methods (both within and outside of class +Array+) * accept one or more non-integer arguments that are * {integer-convertible objects}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]. * * * == Creating Arrays * - * You can create an \Array object explicitly with: + * You can create an +Array+ object explicitly with: * * - An {array literal}[rdoc-ref:literals.rdoc@Array+Literals]: * @@ -8431,7 +8241,7 @@ rb_ary_deconstruct(VALUE ary) * == Example Usage * * In addition to the methods it mixes in through the Enumerable module, the - * \Array class has proprietary methods for accessing, searching and otherwise + * +Array+ class has proprietary methods for accessing, searching and otherwise * manipulating arrays. * * Some of the more common ones are illustrated below. @@ -8479,7 +8289,7 @@ rb_ary_deconstruct(VALUE ary) * * arr.drop(3) #=> [4, 5, 6] * - * == Obtaining Information about an \Array + * == Obtaining Information about an +Array+ * * Arrays keep track of their own length at all times. To query an array * about the number of elements it contains, use #length, #count or #size. @@ -8517,7 +8327,7 @@ rb_ary_deconstruct(VALUE ary) * arr.insert(3, 'orange', 'pear', 'grapefruit') * #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6] * - * == Removing Items from an \Array + * == Removing Items from an +Array+ * * The method #pop removes the last element in an array and returns it: * @@ -8559,9 +8369,9 @@ rb_ary_deconstruct(VALUE ary) * * == Iterating over Arrays * - * Like all classes that include the Enumerable module, \Array has an each + * Like all classes that include the Enumerable module, +Array+ has an each * method, which defines what elements should be iterated over and how. In - * case of Array's #each, all elements in the \Array instance are yielded to + * case of Array's #each, all elements in the +Array+ instance are yielded to * the supplied block in sequence. * * Note that this operation leaves the array unchanged. @@ -8588,7 +8398,7 @@ rb_ary_deconstruct(VALUE ary) * arr #=> [1, 4, 9, 16, 25] * * - * == Selecting Items from an \Array + * == Selecting Items from an +Array+ * * Elements can be selected from an array according to criteria defined in a * block. The selection can happen in a destructive or a non-destructive @@ -8621,13 +8431,13 @@ rb_ary_deconstruct(VALUE ary) * * == What's Here * - * First, what's elsewhere. \Class \Array: + * First, what's elsewhere. \Class +Array+: * * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here]. * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here], * which provides dozens of additional methods. * - * Here, class \Array provides methods that are useful for: + * Here, class +Array+ provides methods that are useful for: * * - {Creating an Array}[rdoc-ref:Array@Methods+for+Creating+an+Array] * - {Querying}[rdoc-ref:Array@Methods+for+Querying] @@ -8640,7 +8450,7 @@ rb_ary_deconstruct(VALUE ary) * - {Converting}[rdoc-ref:Array@Methods+for+Converting] * - {And more....}[rdoc-ref:Array@Other+Methods] * - * === Methods for Creating an \Array + * === Methods for Creating an +Array+ * * - ::[]: Returns a new array populated with given objects. * - ::new: Returns a new array. @@ -8733,7 +8543,7 @@ rb_ary_deconstruct(VALUE ary) * * - #pop: Removes and returns the last element. * - #shift: Removes and returns the first element. - * - #compact!: Removes all non-+nil+ elements. + * - #compact!: Removes all +nil+ elements. * - #delete: Removes elements equal to a given object. * - #delete_at: Removes the element at a given offset. * - #delete_if: Removes elements specified by a given block. @@ -8830,8 +8640,6 @@ Init_Array(void) rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1); rb_define_method(rb_cArray, "at", rb_ary_at, 1); rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1); - rb_define_method(rb_cArray, "first", rb_ary_first, -1); - rb_define_method(rb_cArray, "last", rb_ary_last, -1); rb_define_method(rb_cArray, "concat", rb_ary_concat_multi, -1); rb_define_method(rb_cArray, "union", rb_ary_union_multi, -1); rb_define_method(rb_cArray, "difference", rb_ary_difference_multi, -1); @@ -8845,7 +8653,6 @@ Init_Array(void) rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1); rb_define_alias(rb_cArray, "prepend", "unshift"); rb_define_method(rb_cArray, "insert", rb_ary_insert, -1); - rb_define_method(rb_cArray, "each", rb_ary_each, 0); rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0); rb_define_method(rb_cArray, "reverse_each", rb_ary_reverse_each, 0); rb_define_method(rb_cArray, "length", rb_ary_length, 0); |