2 ** array.c - Array class
4 ** See Copyright Notice in mruby.h
8 #include <mruby/array.h>
9 #include <mruby/class.h>
10 #include <mruby/string.h>
11 #include <mruby/range.h>
12 #include <mruby/proc.h>
13 #include <mruby/internal.h>
14 #include <mruby/presym.h>
15 #include "value_array.h"
17 #define ARY_DEFAULT_LEN 4
18 #define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
19 #define ARY_C_MAX_SIZE (SIZE_MAX / sizeof(mrb_value))
20 #ifndef MRB_ARY_LENGTH_MAX
21 #define MRB_ARY_LENGTH_MAX 131072
23 #define ARY_MAX_SIZE ((mrb_int)((ARY_C_MAX_SIZE < (size_t)MRB_INT_MAX) ? ARY_C_MAX_SIZE : MRB_INT_MAX-1))
26 ary_too_big(mrb_state
*mrb
)
28 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "array size too big");
32 ary_check_too_big(mrb_state
*mrb
, mrb_int a
, mrb_int b
)
34 if (a
> ARY_MAX_SIZE
- b
|| a
< 0)
36 #if MRB_ARY_LENGTH_MAX != 0
37 if (a
> MRB_ARY_LENGTH_MAX
- b
|| a
< 0)
43 ary_new_capa(mrb_state
*mrb
, mrb_int capa
)
45 ary_check_too_big(mrb
, capa
, 0);
47 size_t blen
= capa
* sizeof(mrb_value
);
48 struct RArray
*a
= MRB_OBJ_ALLOC(mrb
, MRB_TT_ARRAY
, mrb
->array_class
);
50 if (capa
<= MRB_ARY_EMBED_LEN_MAX
) {
51 ARY_SET_EMBED_LEN(a
, 0);
54 a
->as
.heap
.ptr
= (mrb_value
*)mrb_malloc(mrb
, blen
);
55 a
->as
.heap
.aux
.capa
= capa
;
63 mrb_ary_new_capa(mrb_state
*mrb
, mrb_int capa
)
65 struct RArray
*a
= ary_new_capa(mrb
, capa
);
66 return mrb_obj_value(a
);
70 mrb_ary_new(mrb_state
*mrb
)
72 return mrb_ary_new_capa(mrb
, 0);
76 * To copy array, use this instead of memcpy because of portability
77 * * gcc on ARM may fail optimization of memcpy
78 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56620
79 * * gcc on MIPS also fail
80 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39755
81 * * memcpy doesn't exist on freestanding environment
83 * If you optimize for binary size, use memcpy instead of this at your own risk
84 * of above portability issue.
86 * See also https://togetter.com/li/462898 (Japanese)
89 array_copy(mrb_value
*dst
, const mrb_value
*src
, mrb_int size
)
91 for (mrb_int i
= 0; i
< size
; i
++) {
97 ary_new_from_values(mrb_state
*mrb
, mrb_int size
, const mrb_value
*vals
)
99 struct RArray
*a
= ary_new_capa(mrb
, size
);
101 array_copy(ARY_PTR(a
), vals
, size
);
102 ARY_SET_LEN(a
, size
);
108 mrb_ary_new_from_values(mrb_state
*mrb
, mrb_int size
, const mrb_value
*vals
)
110 struct RArray
*a
= ary_new_from_values(mrb
, size
, vals
);
111 return mrb_obj_value(a
);
115 mrb_assoc_new(mrb_state
*mrb
, mrb_value car
, mrb_value cdr
)
117 struct RArray
*a
= ary_new_capa(mrb
, 2);
118 mrb_value
*p
= ARY_PTR(a
);
123 return mrb_obj_value(a
);
127 ary_fill_with_nil(mrb_value
*ptr
, mrb_int size
)
129 mrb_value nil
= mrb_nil_value();
136 #define ary_modify_check(mrb, a) mrb_check_frozen((mrb), (a))
139 ary_modify(mrb_state
*mrb
, struct RArray
*a
)
141 ary_modify_check(mrb
, a
);
143 if (ARY_SHARED_P(a
)) {
144 mrb_shared_array
*shared
= a
->as
.heap
.aux
.shared
;
146 if (shared
->refcnt
== 1 && a
->as
.heap
.ptr
== shared
->ptr
) {
147 a
->as
.heap
.ptr
= shared
->ptr
;
148 a
->as
.heap
.aux
.capa
= a
->as
.heap
.len
;
149 mrb_free(mrb
, shared
);
152 mrb_value
*p
= a
->as
.heap
.ptr
;
153 mrb_value
*ptr
= (mrb_value
*)mrb_malloc(mrb
, a
->as
.heap
.len
* sizeof(mrb_value
));
156 array_copy(ptr
, p
, a
->as
.heap
.len
);
158 a
->as
.heap
.ptr
= ptr
;
159 a
->as
.heap
.aux
.capa
= a
->as
.heap
.len
;
160 mrb_ary_decref(mrb
, shared
);
162 ARY_UNSET_SHARED_FLAG(a
);
167 mrb_ary_modify(mrb_state
*mrb
, struct RArray
* a
)
169 mrb_write_barrier(mrb
, (struct RBasic
*)a
);
174 ary_make_shared(mrb_state
*mrb
, struct RArray
*a
)
176 if (!ARY_SHARED_P(a
) && !ARY_EMBED_P(a
)) {
177 mrb_shared_array
*shared
= (mrb_shared_array
*)mrb_malloc(mrb
, sizeof(mrb_shared_array
));
178 mrb_value
*ptr
= a
->as
.heap
.ptr
;
179 mrb_int len
= a
->as
.heap
.len
;
182 if (a
->as
.heap
.aux
.capa
> len
) {
183 a
->as
.heap
.ptr
= shared
->ptr
= (mrb_value
*)mrb_realloc(mrb
, ptr
, sizeof(mrb_value
)*len
+1);
189 a
->as
.heap
.aux
.shared
= shared
;
190 ARY_SET_SHARED_FLAG(a
);
195 ary_expand_capa(mrb_state
*mrb
, struct RArray
*a
, mrb_int len
)
197 mrb_int capa
= ARY_CAPA(a
);
199 ary_check_too_big(mrb
, len
, 0);
200 if (capa
< ARY_DEFAULT_LEN
) {
201 capa
= ARY_DEFAULT_LEN
;
204 if (capa
<= ARY_MAX_SIZE
/ 2) {
211 if (capa
> ARY_MAX_SIZE
) {
215 if (ARY_EMBED_P(a
)) {
216 mrb_value
*ptr
= ARY_EMBED_PTR(a
);
217 mrb_int slen
= ARY_EMBED_LEN(a
);
218 mrb_value
*expanded_ptr
= (mrb_value
*)mrb_malloc(mrb
, sizeof(mrb_value
)*capa
);
220 ARY_UNSET_EMBED_FLAG(a
);
221 array_copy(expanded_ptr
, ptr
, slen
);
222 a
->as
.heap
.len
= slen
;
223 a
->as
.heap
.aux
.capa
= capa
;
224 a
->as
.heap
.ptr
= expanded_ptr
;
226 else if (capa
> a
->as
.heap
.aux
.capa
) {
227 mrb_value
*expanded_ptr
= (mrb_value
*)mrb_realloc(mrb
, a
->as
.heap
.ptr
, sizeof(mrb_value
)*capa
);
229 a
->as
.heap
.aux
.capa
= capa
;
230 a
->as
.heap
.ptr
= expanded_ptr
;
235 ary_shrink_capa(mrb_state
*mrb
, struct RArray
*a
)
237 if (ARY_EMBED_P(a
)) return;
239 mrb_int capa
= a
->as
.heap
.aux
.capa
;
241 if (capa
< ARY_DEFAULT_LEN
* 2) return;
242 if (capa
<= a
->as
.heap
.len
* ARY_SHRINK_RATIO
) return;
246 if (capa
< ARY_DEFAULT_LEN
) {
247 capa
= ARY_DEFAULT_LEN
;
250 } while (capa
> a
->as
.heap
.len
* ARY_SHRINK_RATIO
);
252 if (capa
> a
->as
.heap
.len
&& capa
< a
->as
.heap
.aux
.capa
) {
253 a
->as
.heap
.aux
.capa
= capa
;
254 a
->as
.heap
.ptr
= (mrb_value
*)mrb_realloc(mrb
, a
->as
.heap
.ptr
, sizeof(mrb_value
)*capa
);
259 mrb_ary_resize(mrb_state
*mrb
, mrb_value ary
, mrb_int new_len
)
261 struct RArray
*a
= mrb_ary_ptr(ary
);
264 mrb_int old_len
= RARRAY_LEN(ary
);
265 if (old_len
!= new_len
) {
266 if (new_len
< old_len
) {
267 ary_shrink_capa(mrb
, a
);
270 ary_expand_capa(mrb
, a
, new_len
);
271 ary_fill_with_nil(ARY_PTR(a
) + old_len
, new_len
- old_len
);
273 ARY_SET_LEN(a
, new_len
);
280 mrb_ary_s_create(mrb_state
*mrb
, mrb_value klass
)
282 const mrb_value
*vals
;
285 mrb_get_args(mrb
, "*!", &vals
, &len
);
286 mrb_value ary
= mrb_ary_new_from_values(mrb
, len
, vals
);
287 struct RArray
*a
= mrb_ary_ptr(ary
);
288 a
->c
= mrb_class_ptr(klass
);
293 static void ary_replace(mrb_state
*, struct RArray
*, struct RArray
*);
296 mrb_ary_init(mrb_state
*mrb
, mrb_value ary
)
298 mrb_value ss
= mrb_fixnum_value(0);
299 mrb_value obj
= mrb_nil_value();
300 mrb_value blk
= mrb_nil_value();
302 mrb_get_args(mrb
, "|oo&", &ss
, &obj
, &blk
);
304 if (mrb_array_p(ss
) && mrb_nil_p(obj
) && mrb_nil_p(blk
)) {
305 ary_replace(mrb
, mrb_ary_ptr(ary
), mrb_ary_ptr(ss
));
309 mrb_int size
= mrb_as_int(mrb
, ss
);
310 struct RArray
*a
= mrb_ary_ptr(ary
);
312 if (ARY_CAPA(a
) < size
) {
313 ary_expand_capa(mrb
, a
, size
);
316 int ai
= mrb_gc_arena_save(mrb
);
317 for (mrb_int i
=0; i
<size
; i
++) {
319 if (mrb_nil_p(blk
)) {
323 val
= mrb_yield(mrb
, blk
, mrb_fixnum_value(i
));
325 mrb_ary_set(mrb
, ary
, i
, val
);
326 mrb_gc_arena_restore(mrb
, ai
); // for mrb_funcall
332 ary_concat(mrb_state
*mrb
, struct RArray
*a
, struct RArray
*a2
)
334 mrb_int len
= ARY_LEN(a
);
337 ary_replace(mrb
, a
, a2
);
341 mrb_int len2
= ARY_LEN(a2
);
342 ary_check_too_big(mrb
, len2
, len
);
345 mrb_int newlen
= len
+ len2
;
346 if (ARY_CAPA(a
) < newlen
) {
347 ary_expand_capa(mrb
, a
, newlen
);
349 array_copy(ARY_PTR(a
)+len
, ARY_PTR(a2
), len2
);
350 mrb_write_barrier(mrb
, (struct RBasic
*)a
);
351 ARY_SET_LEN(a
, newlen
);
355 mrb_ary_concat(mrb_state
*mrb
, mrb_value self
, mrb_value other
)
357 struct RArray
*a2
= mrb_ary_ptr(other
);
359 ary_concat(mrb
, mrb_ary_ptr(self
), a2
);
364 * array.concat(*other_arrays) -> self
366 * Adds to +array+ all elements from each \Array in +other_arrays+; returns +self+:
369 * a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5]
373 mrb_ary_concat_m(mrb_state
*mrb
, mrb_value self
)
378 mrb_get_args(mrb
, "*!", &args
, &len
);
379 for (int i
=0; i
<len
; i
++) {
380 mrb_ensure_array_type(mrb
, args
[i
]);
382 for (int i
=0; i
<len
; i
++) {
383 mrb_ary_concat(mrb
, self
, args
[i
]);
389 mrb_ary_plus(mrb_state
*mrb
, mrb_value self
)
391 struct RArray
*a1
= mrb_ary_ptr(self
);
392 const mrb_value
*ptr
;
395 mrb_get_args(mrb
, "a", &ptr
, &blen
);
396 ary_check_too_big(mrb
, ARY_LEN(a1
), blen
);
397 mrb_int len1
= ARY_LEN(a1
);
398 struct RArray
*a2
= ary_new_capa(mrb
, len1
+ blen
);
399 array_copy(ARY_PTR(a2
), ARY_PTR(a1
), len1
);
400 array_copy(ARY_PTR(a2
) + len1
, ptr
, blen
);
401 ARY_SET_LEN(a2
, len1
+blen
);
403 return mrb_obj_value(a2
);
406 #define ARY_REPLACE_SHARED_MIN 20
409 ary_replace(mrb_state
*mrb
, struct RArray
*a
, struct RArray
*b
)
411 mrb_int len
= ARY_LEN(b
);
413 ary_modify_check(mrb
, a
);
415 if (ARY_SHARED_P(a
)) {
416 mrb_ary_decref(mrb
, a
->as
.heap
.aux
.shared
);
417 a
->as
.heap
.aux
.capa
= 0;
419 a
->as
.heap
.ptr
= NULL
;
420 ARY_UNSET_SHARED_FLAG(a
);
422 if (ARY_SHARED_P(b
)) {
424 if (ARY_EMBED_P(a
)) {
425 ARY_UNSET_EMBED_FLAG(a
);
428 mrb_free(mrb
, a
->as
.heap
.ptr
);
430 a
->as
.heap
.ptr
= b
->as
.heap
.ptr
;
431 a
->as
.heap
.len
= len
;
432 a
->as
.heap
.aux
.shared
= b
->as
.heap
.aux
.shared
;
433 a
->as
.heap
.aux
.shared
->refcnt
++;
434 ARY_SET_SHARED_FLAG(a
);
435 mrb_write_barrier(mrb
, (struct RBasic
*)a
);
438 if (!mrb_frozen_p(b
) && len
> ARY_REPLACE_SHARED_MIN
) {
439 ary_make_shared(mrb
, b
);
442 if (ARY_CAPA(a
) < len
)
443 ary_expand_capa(mrb
, a
, len
);
444 array_copy(ARY_PTR(a
), ARY_PTR(b
), len
);
445 mrb_write_barrier(mrb
, (struct RBasic
*)a
);
450 mrb_ary_replace(mrb_state
*mrb
, mrb_value self
, mrb_value other
)
452 struct RArray
*a1
= mrb_ary_ptr(self
);
453 struct RArray
*a2
= mrb_ary_ptr(other
);
456 ary_replace(mrb
, a1
, a2
);
461 mrb_ary_replace_m(mrb_state
*mrb
, mrb_value self
)
465 mrb_get_args(mrb
, "A", &other
);
466 mrb_ary_replace(mrb
, self
, other
);
472 mrb_ary_times(mrb_state
*mrb
, mrb_value self
)
474 struct RArray
*a1
= mrb_ary_ptr(self
);
476 mrb_value arg
= mrb_get_arg1(mrb
);
477 mrb_value tmp
= mrb_check_string_type(mrb
, arg
);
478 if (!mrb_nil_p(tmp
)) {
479 return mrb_ary_join(mrb
, self
, tmp
);
482 mrb_int times
= mrb_as_int(mrb
, arg
);
484 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "negative argument");
486 if (times
== 0) return mrb_ary_new(mrb
);
487 if (ARY_MAX_SIZE
/ times
< ARY_LEN(a1
)) {
491 mrb_int len1
= ARY_LEN(a1
);
492 struct RArray
*a2
= ary_new_capa(mrb
, len1
* times
);
493 ARY_SET_LEN(a2
, len1
* times
);
495 mrb_value
*ptr
= ARY_PTR(a2
);
497 array_copy(ptr
, ARY_PTR(a1
), len1
);
501 return mrb_obj_value(a2
);
505 mrb_ary_reverse_bang(mrb_state
*mrb
, mrb_value self
)
507 struct RArray
*a
= mrb_ary_ptr(self
);
508 mrb_int len
= ARY_LEN(a
);
513 mrb_value
*p1
= ARY_PTR(a
);
514 mrb_value
*p2
= p1
+ len
- 1;
526 mrb_ary_reverse(mrb_state
*mrb
, mrb_value self
)
528 struct RArray
*a
= mrb_ary_ptr(self
), *b
= ary_new_capa(mrb
, ARY_LEN(a
));
529 mrb_int len
= ARY_LEN(a
);
532 mrb_value
*p1
= ARY_PTR(a
);
533 mrb_value
*e
= p1
+ len
;
534 mrb_value
*p2
= ARY_PTR(b
) + len
- 1;
540 return mrb_obj_value(b
);
544 mrb_ary_push(mrb_state
*mrb
, mrb_value ary
, mrb_value elem
)
546 struct RArray
*a
= mrb_ary_ptr(ary
);
547 mrb_int len
= ARY_LEN(a
);
550 if (len
== ARY_CAPA(a
))
551 ary_expand_capa(mrb
, a
, len
+ 1);
552 ARY_PTR(a
)[len
] = elem
;
553 ARY_SET_LEN(a
, len
+1);
554 mrb_field_write_barrier_value(mrb
, (struct RBasic
*)a
, elem
);
558 mrb_ary_push_m(mrb_state
*mrb
, mrb_value self
)
560 mrb_int argc
= mrb_get_argc(mrb
);
562 mrb_ary_push(mrb
, self
, mrb_get_argv(mrb
)[0]);
565 struct RArray
*a
= mrb_ary_ptr(self
);
566 mrb_int len
= ARY_LEN(a
);
567 mrb_int len2
= len
+ argc
;
569 if (ARY_CAPA(a
) < len2
) {
570 ary_expand_capa(mrb
, a
, len2
);
572 const mrb_value
*argv
= mrb_get_argv(mrb
);
573 array_copy(ARY_PTR(a
)+len
, argv
, argc
);
574 ARY_SET_LEN(a
, len2
);
576 mrb_field_write_barrier_value(mrb
, (struct RBasic
*)a
, *argv
);
583 mrb_ary_pop(mrb_state
*mrb
, mrb_value ary
)
585 struct RArray
*a
= mrb_ary_ptr(ary
);
586 mrb_int len
= ARY_LEN(a
);
588 ary_modify_check(mrb
, a
);
589 if (len
== 0) return mrb_nil_value();
590 ARY_SET_LEN(a
, len
-1);
591 return ARY_PTR(a
)[len
-1];
594 #define ARY_SHIFT_SHARED_MIN 10
597 mrb_ary_shift(mrb_state
*mrb
, mrb_value self
)
599 struct RArray
*a
= mrb_ary_ptr(self
);
600 mrb_int len
= ARY_LEN(a
);
602 ary_modify_check(mrb
, a
);
603 if (len
== 0) return mrb_nil_value();
604 if (ARY_SHARED_P(a
)) {
608 return a
->as
.heap
.ptr
[-1];
610 else if (len
> ARY_SHIFT_SHARED_MIN
) {
611 ary_make_shared(mrb
, a
);
615 mrb_value
*ptr
= ARY_PTR(a
);
617 mrb_value val
= *ptr
;
623 ARY_SET_LEN(a
, len
-1);
629 mrb_ary_shift_m(mrb_state
*mrb
, mrb_value self
)
632 if (mrb_get_argc(mrb
) == 0) {
633 return mrb_ary_shift(mrb
, self
);
636 mrb_int n
= mrb_as_int(mrb
, mrb_get_arg1(mrb
));
637 struct RArray
*a
= mrb_ary_ptr(self
);
638 mrb_int len
= ARY_LEN(a
);
640 ary_modify_check(mrb
, a
);
641 if (len
== 0 || n
== 0) return mrb_ary_new(mrb
);
642 if (n
< 0) mrb_raise(mrb
, E_ARGUMENT_ERROR
, "negative array shift");
643 if (n
> len
) n
= len
;
644 mrb_value val
= mrb_ary_new_from_values(mrb
, n
, ARY_PTR(a
));
645 if (ARY_SHARED_P(a
)) {
651 if (len
> ARY_SHIFT_SHARED_MIN
) {
652 ary_make_shared(mrb
, a
);
659 mrb_value
*ptr
= ARY_PTR(a
);
660 mrb_int size
= len
-n
;
666 ARY_SET_LEN(a
, len
-n
);
674 p self #=> [0, 1, 2, 3] */
676 mrb_ary_unshift(mrb_state
*mrb
, mrb_value self
, mrb_value item
)
678 struct RArray
*a
= mrb_ary_ptr(self
);
679 mrb_int len
= ARY_LEN(a
);
682 && a
->as
.heap
.aux
.shared
->refcnt
== 1 /* shared only referenced from this array */
683 && a
->as
.heap
.ptr
- a
->as
.heap
.aux
.shared
->ptr
>= 1) /* there's room for unshifted item */ {
685 a
->as
.heap
.ptr
[0] = item
;
691 if (ARY_CAPA(a
) < len
+ 1)
692 ary_expand_capa(mrb
, a
, len
+ 1);
694 value_move(ptr
+ 1, ptr
, len
);
697 ARY_SET_LEN(a
, len
+1);
698 mrb_field_write_barrier_value(mrb
, (struct RBasic
*)a
, item
);
705 * array.unshift(*objects) -> self
707 * Prepends the given +objects+ to +self+:
709 * a = [:foo, 'bar', 2]
710 * a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
712 * Array#prepend is an alias for Array#unshift.
714 * Related: #push, #pop, #shift.
718 mrb_ary_unshift_m(mrb_state
*mrb
, mrb_value self
)
720 struct RArray
*a
= mrb_ary_ptr(self
);
723 mrb_int alen
= mrb_get_argc(mrb
);
726 ary_modify_check(mrb
, a
);
729 const mrb_value
*vals
= mrb_get_argv(mrb
);
730 mrb_int len
= ARY_LEN(a
);
731 if (alen
> ARY_MAX_SIZE
- len
) {
735 && a
->as
.heap
.aux
.shared
->refcnt
== 1 /* shared only referenced from this array */
736 && a
->as
.heap
.ptr
- a
->as
.heap
.aux
.shared
->ptr
>= alen
) /* there's room for unshifted item */ {
737 ary_modify_check(mrb
, a
);
738 a
->as
.heap
.ptr
-= alen
;
739 ptr
= a
->as
.heap
.ptr
;
742 mrb_bool same
= vals
== ARY_PTR(a
);
744 if (ARY_CAPA(a
) < len
+ alen
)
745 ary_expand_capa(mrb
, a
, len
+ alen
);
747 value_move(ptr
+ alen
, ptr
, len
);
748 if (same
) vals
= ptr
;
750 array_copy(ptr
, vals
, alen
);
751 ARY_SET_LEN(a
, len
+alen
);
753 mrb_field_write_barrier_value(mrb
, (struct RBasic
*)a
, vals
[alen
]);
760 mrb_ary_set(mrb_state
*mrb
, mrb_value ary
, mrb_int n
, mrb_value val
)
762 struct RArray
*a
= mrb_ary_ptr(ary
);
763 mrb_int len
= ARY_LEN(a
);
770 mrb_raisef(mrb
, E_INDEX_ERROR
, "index %i out of array", n
- len
);
773 if (n
>= ARY_MAX_SIZE
) {
774 mrb_raise(mrb
, E_INDEX_ERROR
, "index too big");
777 if (ARY_CAPA(a
) <= n
)
778 ary_expand_capa(mrb
, a
, n
+ 1);
779 ary_fill_with_nil(ARY_PTR(a
) + len
, n
+ 1 - len
);
784 mrb_field_write_barrier_value(mrb
, (struct RBasic
*)a
, val
);
787 static struct RArray
*
788 ary_dup(mrb_state
*mrb
, struct RArray
*a
)
790 return ary_new_from_values(mrb
, ARY_LEN(a
), ARY_PTR(a
));
794 mrb_ary_splice(mrb_state
*mrb
, mrb_value ary
, mrb_int head
, mrb_int len
, mrb_value rpl
)
796 struct RArray
*a
= mrb_ary_ptr(ary
);
797 mrb_int alen
= ARY_LEN(a
);
798 const mrb_value
*argv
;
805 if (len
< 0) mrb_raisef(mrb
, E_INDEX_ERROR
, "negative length (%i)", len
);
810 if (head
< 0) goto out_of_range
;
812 if (head
> ARY_MAX_SIZE
- len
) {
814 mrb_raisef(mrb
, E_INDEX_ERROR
, "index %i is out of array", head
);
817 if (alen
< len
|| alen
< tail
) {
823 if (mrb_array_p(rpl
)) {
824 argc
= RARRAY_LEN(rpl
);
825 argv
= RARRAY_PTR(rpl
);
826 if (argv
== ARY_PTR(a
)) {
830 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "too big recursive splice");
836 else if (mrb_undef_p(rpl
)) {
845 if (head
> ARY_MAX_SIZE
- argc
) goto out_of_range
;
847 if (len
> ARY_CAPA(a
)) {
848 ary_expand_capa(mrb
, a
, len
);
850 ary_fill_with_nil(ARY_PTR(a
) + alen
, head
- alen
);
852 array_copy(ARY_PTR(a
) + head
, argv
, argc
);
858 if (alen
- len
> ARY_MAX_SIZE
- argc
) {
859 head
= alen
+ argc
- len
;
862 mrb_int newlen
= alen
+ argc
- len
;
863 if (newlen
> ARY_CAPA(a
)) {
864 ary_expand_capa(mrb
, a
, newlen
);
868 mrb_value
*ptr
= ARY_PTR(a
);
869 value_move(ptr
+ head
+ argc
, ptr
+ tail
, alen
- tail
);
870 ARY_SET_LEN(a
, newlen
);
873 value_move(ARY_PTR(a
) + head
, argv
, argc
);
876 mrb_write_barrier(mrb
, (struct RBasic
*)a
);
881 mrb_ary_decref(mrb_state
*mrb
, mrb_shared_array
*shared
)
884 if (shared
->refcnt
== 0) {
885 mrb_free(mrb
, shared
->ptr
);
886 mrb_free(mrb
, shared
);
891 ary_subseq(mrb_state
*mrb
, struct RArray
*a
, mrb_int beg
, mrb_int len
)
895 if (!ARY_SHARED_P(a
) && len
<= ARY_SHIFT_SHARED_MIN
) {
896 return mrb_ary_new_from_values(mrb
, len
, ARY_PTR(a
)+beg
);
898 ary_make_shared(mrb
, a
);
899 b
= MRB_OBJ_ALLOC(mrb
, MRB_TT_ARRAY
, mrb
->array_class
);
900 b
->as
.heap
.ptr
= a
->as
.heap
.ptr
+ beg
;
901 b
->as
.heap
.len
= len
;
902 b
->as
.heap
.aux
.shared
= a
->as
.heap
.aux
.shared
;
903 b
->as
.heap
.aux
.shared
->refcnt
++;
904 ARY_SET_SHARED_FLAG(b
);
906 return mrb_obj_value(b
);
910 mrb_ary_subseq(mrb_state
*mrb
, mrb_value ary
, mrb_int beg
, mrb_int len
)
912 struct RArray
*a
= mrb_ary_ptr(ary
);
913 return ary_subseq(mrb
, a
, beg
, len
);
917 aget_index(mrb_state
*mrb
, mrb_value index
)
919 if (mrb_integer_p(index
)) {
920 return mrb_integer(index
);
923 else if (mrb_float_p(index
)) {
924 return (mrb_int
)mrb_float(index
);
929 const mrb_value
*argv
;
931 mrb_get_args(mrb
, "i*!", &i
, &argv
, &argc
);
938 * ary[index] -> obj or nil
939 * ary[start, length] -> new_ary or nil
940 * ary[range] -> new_ary or nil
941 * ary.slice(index) -> obj or nil
942 * ary.slice(start, length) -> new_ary or nil
943 * ary.slice(range) -> new_ary or nil
945 * Element Reference --- Returns the element at +index+, or returns a
946 * subarray starting at the +start+ index and continuing for +length+
947 * elements, or returns a subarray specified by +range+ of indices.
949 * Negative indices count backward from the end of the array (-1 is the last
950 * element). For +start+ and +range+ cases the starting index is just before
951 * an element. Additionally, an empty array is returned when the starting
952 * index for an element range is at the end of the array.
954 * Returns +nil+ if the index (or starting index) are out of range.
956 * a = [ "a", "b", "c", "d", "e" ]
958 * a[1,2] => ["b", "c"]
959 * a[1..-2] => ["b", "c", "d"]
964 mrb_ary_aget(mrb_state
*mrb
, mrb_value self
)
966 struct RArray
*a
= mrb_ary_ptr(self
);
970 if (mrb_get_argc(mrb
) == 1) {
971 index
= mrb_get_arg1(mrb
);
972 switch (mrb_type(index
)) {
975 if (mrb_range_beg_len(mrb
, index
, &i
, &len
, ARY_LEN(a
), TRUE
) == MRB_RANGE_OK
) {
976 return ary_subseq(mrb
, a
, i
, len
);
979 return mrb_nil_value();
982 return mrb_ary_ref(mrb
, self
, mrb_integer(index
));
984 return mrb_ary_ref(mrb
, self
, aget_index(mrb
, index
));
988 mrb_get_args(mrb
, "oi", &index
, &len
);
989 i
= aget_index(mrb
, index
);
990 mrb_int alen
= ARY_LEN(a
);
991 if (i
< 0) i
+= alen
;
992 if (i
< 0 || alen
< i
) return mrb_nil_value();
993 if (len
< 0) return mrb_nil_value();
994 if (alen
== i
) return mrb_ary_new(mrb
);
995 if (len
> alen
- i
) len
= alen
- i
;
997 return ary_subseq(mrb
, a
, i
, len
);
1002 * ary[index] = obj -> obj
1003 * ary[start, length] = obj or other_ary or nil -> obj or other_ary or nil
1004 * ary[range] = obj or other_ary or nil -> obj or other_ary or nil
1006 * Element Assignment --- Sets the element at +index+, or replaces a subarray
1007 * from the +start+ index for +length+ elements, or replaces a subarray
1008 * specified by the +range+ of indices.
1010 * If indices are greater than the current capacity of the array, the array
1011 * grows automatically. Elements are inserted into the array at +start+ if
1014 * Negative indices will count backward from the end of the array. For
1015 * +start+ and +range+ cases the starting index is just before an element.
1017 * An IndexError is raised if a negative index points past the beginning of
1020 * See also Array#push, and Array#unshift.
1023 * a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
1024 * a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
1025 * a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
1026 * a[0, 2] = "?" #=> ["?", 2, nil, "4"]
1027 * a[0..2] = "A" #=> ["A", "4"]
1028 * a[-1] = "Z" #=> ["A", "Z"]
1029 * a[1..-1] = nil #=> ["A", nil]
1030 * a[1..-1] = [] #=> ["A"]
1031 * a[0, 0] = [ 1, 2 ] #=> [1, 2, "A"]
1032 * a[3, 0] = "B" #=> [1, 2, "A", "B"]
1036 mrb_ary_aset(mrb_state
*mrb
, mrb_value self
)
1038 mrb_value v1
, v2
, v3
;
1040 if (mrb_get_argc(mrb
) == 2) {
1042 const mrb_value
*vs
= mrb_get_argv(mrb
);
1043 v1
= vs
[0]; v2
= vs
[1];
1046 switch (mrb_range_beg_len(mrb
, v1
, &i
, &len
, RARRAY_LEN(self
), FALSE
)) {
1047 case MRB_RANGE_TYPE_MISMATCH
:
1048 mrb_ary_set(mrb
, self
, aget_index(mrb
, v1
), v2
);
1051 mrb_ary_splice(mrb
, self
, i
, len
, v2
);
1054 mrb_raisef(mrb
, E_RANGE_ERROR
, "%v out of range", v1
);
1060 mrb_get_args(mrb
, "ooo", &v1
, &v2
, &v3
);
1062 mrb_ary_splice(mrb
, self
, aget_index(mrb
, v1
), aget_index(mrb
, v2
), v3
);
1067 mrb_ary_delete_at(mrb_state
*mrb
, mrb_value self
)
1069 struct RArray
*a
= mrb_ary_ptr(self
);
1071 mrb_int index
= mrb_as_int(mrb
, mrb_get_arg1(mrb
));
1072 mrb_int alen
= ARY_LEN(a
);
1073 if (index
< 0) index
+= alen
;
1074 if (index
< 0 || alen
<= index
) return mrb_nil_value();
1077 mrb_value
*ptr
= ARY_PTR(a
);
1078 mrb_value val
= ptr
[index
];
1081 mrb_int len
= alen
- index
;
1086 ARY_SET_LEN(a
, alen
-1);
1088 ary_shrink_capa(mrb
, a
);
1094 mrb_ary_first(mrb_state
*mrb
, mrb_value self
)
1096 struct RArray
*a
= mrb_ary_ptr(self
);
1099 if (mrb_get_argc(mrb
) == 0) {
1100 if (ARY_LEN(a
) > 0) return ARY_PTR(a
)[0];
1101 return mrb_nil_value();
1103 mrb_get_args(mrb
, "|i", &size
);
1105 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "negative array size");
1108 mrb_int alen
= ARY_LEN(a
);
1109 if (size
> alen
) size
= alen
;
1110 if (ARY_SHARED_P(a
)) {
1111 return ary_subseq(mrb
, a
, 0, size
);
1113 return mrb_ary_new_from_values(mrb
, size
, ARY_PTR(a
));
1117 mrb_ary_last(mrb_state
*mrb
, mrb_value self
)
1119 struct RArray
*a
= mrb_ary_ptr(self
);
1120 mrb_int alen
= ARY_LEN(a
);
1122 if (mrb_get_argc(mrb
) == 0) {
1123 if (alen
> 0) return ARY_PTR(a
)[alen
- 1];
1124 return mrb_nil_value();
1127 mrb_int size
= mrb_integer(mrb_get_arg1(mrb
));
1129 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "negative array size");
1131 if (size
> alen
) size
= alen
;
1132 if (ARY_SHARED_P(a
) || size
> ARY_DEFAULT_LEN
) {
1133 return ary_subseq(mrb
, a
, alen
- size
, size
);
1135 return mrb_ary_new_from_values(mrb
, size
, ARY_PTR(a
) + alen
- size
);
1140 * ary.index(val) -> int or nil
1141 * ary.index {|item| block } -> int or nil
1142 * array.index -> enumerator
1144 * Returns the _index_ of the first object in +ary+ such that the object is
1145 * <code>==</code> to +obj+.
1147 * If a block is given instead of an argument, returns the _index_ of the
1148 * first object for which the block returns +true+. Returns +nil+ if no
1154 mrb_ary_index_m(mrb_state
*mrb
, mrb_value self
)
1158 if (mrb_get_args(mrb
, "|o&", &obj
, &blk
) == 0 && mrb_nil_p(blk
)) {
1159 return mrb_funcall_id(mrb
, self
, MRB_SYM(to_enum
), 1, mrb_symbol_value(MRB_SYM(index
)));
1162 if (mrb_nil_p(blk
)) {
1163 for (mrb_int i
= 0; i
< RARRAY_LEN(self
); i
++) {
1164 if (mrb_equal(mrb
, RARRAY_PTR(self
)[i
], obj
)) {
1165 return mrb_int_value(mrb
, i
);
1170 for (mrb_int i
= 0; i
< RARRAY_LEN(self
); i
++) {
1171 mrb_value eq
= mrb_yield(mrb
, blk
, RARRAY_PTR(self
)[i
]);
1173 return mrb_int_value(mrb
, i
);
1177 return mrb_nil_value();
1182 * ary.rindex(val) -> int or nil
1183 * ary.rindex {|item| block } -> int or nil
1184 * array.rindex -> enumerator
1186 * Returns the _index_ of the first object in +ary+ such that the object is
1187 * <code>==</code> to +obj+.
1189 * If a block is given instead of an argument, returns the _index_ of the
1190 * first object for which the block returns +true+. Returns +nil+ if no
1196 mrb_ary_rindex_m(mrb_state
*mrb
, mrb_value self
)
1200 if (mrb_get_args(mrb
, "|o&", &obj
, &blk
) == 0 && mrb_nil_p(blk
)) {
1201 return mrb_funcall_id(mrb
, self
, MRB_SYM(to_enum
), 1, mrb_symbol_value(MRB_SYM(rindex
)));
1204 for (mrb_int i
= RARRAY_LEN(self
) - 1; i
>= 0; i
--) {
1205 if (mrb_nil_p(blk
)) {
1206 if (mrb_equal(mrb
, RARRAY_PTR(self
)[i
], obj
)) {
1207 return mrb_int_value(mrb
, i
);
1211 mrb_value eq
= mrb_yield(mrb
, blk
, RARRAY_PTR(self
)[i
]);
1212 if (mrb_test(eq
)) return mrb_int_value(mrb
, i
);
1214 mrb_int len
= RARRAY_LEN(self
);
1219 return mrb_nil_value();
1223 mrb_ary_splat(mrb_state
*mrb
, mrb_value v
)
1227 if (mrb_array_p(v
)) {
1228 a
= ary_dup(mrb
, mrb_ary_ptr(v
));
1229 return mrb_obj_value(a
);
1232 if (!mrb_respond_to(mrb
, v
, MRB_SYM(to_a
))) {
1233 return mrb_ary_new_from_values(mrb
, 1, &v
);
1236 mrb_value ary
= mrb_funcall_argv(mrb
, v
, MRB_SYM(to_a
), 0, NULL
);
1237 if (mrb_nil_p(ary
)) {
1238 return mrb_ary_new_from_values(mrb
, 1, &v
);
1240 mrb_ensure_array_type(mrb
, ary
);
1241 a
= mrb_ary_ptr(ary
);
1242 a
= ary_dup(mrb
, a
);
1243 return mrb_obj_value(a
);
1247 mrb_ary_size(mrb_state
*mrb
, mrb_value self
)
1249 struct RArray
*a
= mrb_ary_ptr(self
);
1251 return mrb_int_value(mrb
, ARY_LEN(a
));
1255 mrb_ary_clear(mrb_state
*mrb
, mrb_value self
)
1257 struct RArray
*a
= mrb_ary_ptr(self
);
1260 if (ARY_SHARED_P(a
)) {
1261 mrb_ary_decref(mrb
, a
->as
.heap
.aux
.shared
);
1262 ARY_UNSET_SHARED_FLAG(a
);
1264 else if (!ARY_EMBED_P(a
)){
1265 mrb_free(mrb
, a
->as
.heap
.ptr
);
1267 if (MRB_ARY_EMBED_LEN_MAX
> 0) {
1268 ARY_SET_EMBED_LEN(a
, 0);
1271 a
->as
.heap
.ptr
= NULL
;
1272 a
->as
.heap
.aux
.capa
= 0;
1279 mrb_ary_empty_p(mrb_state
*mrb
, mrb_value self
)
1281 struct RArray
*a
= mrb_ary_ptr(self
);
1283 return mrb_bool_value(ARY_LEN(a
) == 0);
1287 mrb_ary_entry(mrb_value ary
, mrb_int n
)
1289 struct RArray
*a
= mrb_ary_ptr(ary
);
1290 mrb_int len
= ARY_LEN(a
);
1293 if (n
< 0) n
+= len
;
1294 if (n
< 0 || len
<= n
) return mrb_nil_value();
1296 return ARY_PTR(a
)[n
];
1300 join_ary(mrb_state
*mrb
, mrb_value ary
, mrb_value sep
, mrb_value list
)
1302 /* check recursive */
1303 for (mrb_int i
=0; i
<RARRAY_LEN(list
); i
++) {
1304 if (mrb_obj_equal(mrb
, ary
, RARRAY_PTR(list
)[i
])) {
1305 mrb_raise(mrb
, E_ARGUMENT_ERROR
, "recursive array join");
1309 mrb_ary_push(mrb
, list
, ary
);
1311 mrb_value result
= mrb_str_new_capa(mrb
, 64);
1313 for (mrb_int i
=0; i
<RARRAY_LEN(ary
); i
++) {
1314 if (i
> 0 && !mrb_nil_p(sep
)) {
1315 mrb_str_cat_str(mrb
, result
, sep
);
1318 mrb_value val
= RARRAY_PTR(ary
)[i
];
1320 switch (mrb_type(val
)) {
1323 val
= join_ary(mrb
, val
, sep
, list
);
1328 mrb_str_cat_str(mrb
, result
, val
);
1332 if (!mrb_immediate_p(val
)) {
1333 mrb_value tmp
= mrb_check_string_type(mrb
, val
);
1334 if (!mrb_nil_p(tmp
)) {
1338 tmp
= mrb_check_array_type(mrb
, val
);
1339 if (!mrb_nil_p(tmp
)) {
1344 val
= mrb_obj_as_string(mrb
, val
);
1349 mrb_ary_pop(mrb
, list
);
1355 mrb_ary_join(mrb_state
*mrb
, mrb_value ary
, mrb_value sep
)
1357 if (!mrb_nil_p(sep
)) {
1358 sep
= mrb_obj_as_string(mrb
, sep
);
1360 return join_ary(mrb
, ary
, sep
, mrb_ary_new(mrb
));
1365 * ary.join(sep="") -> str
1367 * Returns a string created by converting each element of the array to
1368 * a string, separated by <i>sep</i>.
1370 * [ "a", "b", "c" ].join #=> "abc"
1371 * [ "a", "b", "c" ].join("-") #=> "a-b-c"
1375 mrb_ary_join_m(mrb_state
*mrb
, mrb_value ary
)
1377 mrb_value sep
= mrb_nil_value();
1379 mrb_get_args(mrb
, "|S!", &sep
);
1380 return mrb_ary_join(mrb
, ary
, sep
);
1385 * ary.to_s -> string
1386 * ary.inspect -> string
1388 * Return the contents of this array as a string.
1391 mrb_ary_to_s(mrb_state
*mrb
, mrb_value self
)
1393 mrb
->c
->ci
->mid
= MRB_SYM(inspect
);
1394 mrb_value ret
= mrb_str_new_lit(mrb
, "[");
1395 int ai
= mrb_gc_arena_save(mrb
);
1396 if (mrb_inspect_recursive_p(mrb
, self
)) {
1397 mrb_str_cat_lit(mrb
, ret
, "...]");
1400 for (mrb_int i
=0; i
<RARRAY_LEN(self
); i
++) {
1401 if (i
>0) mrb_str_cat_lit(mrb
, ret
, ", ");
1402 mrb_str_cat_str(mrb
, ret
, mrb_inspect(mrb
, RARRAY_PTR(self
)[i
]));
1403 mrb_gc_arena_restore(mrb
, ai
);
1405 mrb_str_cat_lit(mrb
, ret
, "]");
1410 /* check array equality: 1=equal,0=not_equal,-1=need_elements_check */
1412 ary_eq(mrb_state
*mrb
, mrb_value ary1
, mrb_value ary2
)
1414 if (mrb_obj_equal(mrb
, ary1
, ary2
)) return 1;
1415 if (!mrb_array_p(ary2
)) return 0;
1416 if (RARRAY_LEN(ary1
) != RARRAY_LEN(ary2
)) return 0;
1423 * array == other -> true or false
1425 * Equality---Two arrays are equal if they contain the same number
1426 * of elements and if each element is equal to (according to
1427 * Object.==) the corresponding element in the other array.
1431 mrb_ary_eq(mrb_state
*mrb
, mrb_value ary1
)
1433 mrb_value ary2
= mrb_get_arg1(mrb
);
1434 mrb_int n
= ary_eq(mrb
, ary1
, ary2
);
1436 if (n
== 1) return mrb_true_value();
1437 if (n
== 0) return mrb_false_value();
1439 int ai
= mrb_gc_arena_save(mrb
);
1440 for (mrb_int i
=0; i
<RARRAY_LEN(ary1
); i
++) {
1441 mrb_value eq
= mrb_funcall_id(mrb
, mrb_ary_entry(ary1
, i
), MRB_OPSYM(eq
), 1, mrb_ary_entry(ary2
, i
));
1442 if (!mrb_test(eq
)) return mrb_false_value();
1443 mrb_gc_arena_restore(mrb
, ai
);
1445 return mrb_true_value();
1450 * array.eql? other_array -> true or false
1452 * Returns <code>true</code> if +self+ and _other_ are the same object,
1453 * or are both arrays with the same content.
1457 mrb_ary_eql(mrb_state
*mrb
, mrb_value ary1
)
1459 mrb_value ary2
= mrb_get_arg1(mrb
);
1460 mrb_int n
= ary_eq(mrb
, ary1
, ary2
);
1462 if (n
== 1) return mrb_true_value();
1463 if (n
== 0) return mrb_false_value();
1465 int ai
= mrb_gc_arena_save(mrb
);
1466 for (mrb_int i
=0; i
<RARRAY_LEN(ary1
); i
++) {
1467 mrb_value eq
= mrb_funcall_id(mrb
, mrb_ary_entry(ary1
, i
), MRB_SYM_Q(eql
), 1, mrb_ary_entry(ary2
, i
));
1468 if (!mrb_test(eq
)) return mrb_false_value();
1469 mrb_gc_arena_restore(mrb
, ai
);
1471 return mrb_true_value();
1476 * array <=> other_array -> -1, 0, or 1
1478 * Comparison---Returns an integer (-1, 0, or +1)
1479 * if this array is less than, equal to, or greater than <i>other_ary</i>.
1480 * Each object in each array is compared (using <=>). If any value isn't
1481 * equal, then that inequality is the return value. If all the
1482 * values found are equal, then the return is based on a
1483 * comparison of the array lengths. Thus, two arrays are
1484 * "equal" according to <code>Array*<=></code> if and only if they have
1485 * the same length and the value of each element is equal to the
1486 * value of the corresponding element in the other array.
1489 mrb_ary_cmp(mrb_state
*mrb
, mrb_value ary1
)
1491 mrb_value ary2
= mrb_get_arg1(mrb
);
1493 if (mrb_obj_equal(mrb
, ary1
, ary2
)) return mrb_fixnum_value(0);
1494 if (!mrb_array_p(ary2
)) return mrb_nil_value();
1496 for (mrb_int i
=0; i
<RARRAY_LEN(ary1
) && i
<RARRAY_LEN(ary2
); i
++) {
1497 mrb_int n
= mrb_cmp(mrb
, RARRAY_PTR(ary1
)[i
], RARRAY_PTR(ary2
)[i
]);
1498 if (n
== -2) return mrb_nil_value();
1499 if (n
!= 0) return mrb_fixnum_value(n
);
1501 mrb_int len
= RARRAY_LEN(ary1
) - RARRAY_LEN(ary2
);
1502 if (len
== 0) return mrb_fixnum_value(0);
1503 else if (len
> 0) return mrb_fixnum_value(1);
1504 else return mrb_fixnum_value(-1);
1507 /* internal method to convert multi-value to single value */
1509 mrb_ary_svalue(mrb_state
*mrb
, mrb_value ary
)
1511 switch (RARRAY_LEN(ary
)) {
1513 return mrb_nil_value();
1515 return RARRAY_PTR(ary
)[0];
1523 * array.delete(obj) -> deleted_object
1524 * array.delete(obj) {|nosuch| ... } -> deleted_object or block_return
1526 * Removes zero or more elements from self; returns self.
1528 * When no block is given, removes from self each element e such
1529 * that e == obj; returns the last deleted element
1531 * Returns nil if no elements removed.
1533 * When a block is given, removes from self each element e such
1534 * that e == obj. If any such elements are found, ignores the block and
1535 * returns the last. Otherwise, returns the block's return value.
1538 mrb_ary_delete(mrb_state
*mrb
, mrb_value self
)
1542 mrb_get_args(mrb
, "o&", &obj
, &blk
);
1544 struct RArray
*ary
= RARRAY(self
);
1545 mrb_value ret
= obj
;
1546 int ai
= mrb_gc_arena_save(mrb
);
1549 for (; i
< ARY_LEN(ary
); i
++) {
1550 mrb_value elem
= ARY_PTR(ary
)[i
];
1552 if (mrb_equal(mrb
, elem
, obj
)) {
1553 mrb_gc_arena_restore(mrb
, ai
);
1554 mrb_gc_protect(mrb
, elem
);
1560 if (j
>= ARY_LEN(ary
)) {
1561 // Since breaking here will further change the array length,
1562 // there is no choice but to raise an exception or return.
1563 mrb_raise(mrb
, E_RUNTIME_ERROR
, "array modified during delete");
1565 ary_modify(mrb
, ary
);
1566 ARY_PTR(ary
)[j
] = elem
;
1573 if (mrb_nil_p(blk
)) return mrb_nil_value();
1574 return mrb_yield(mrb
, blk
, obj
);
1577 ARY_SET_LEN(ary
, j
);
1581 static mrb_noreturn
void
1582 cmp_failed(mrb_state
*mrb
, mrb_int a
, mrb_int b
)
1584 mrb_raisef(mrb
, E_ARGUMENT_ERROR
, "comparison failed (element %d and %d)", a
, b
);
1588 sort_cmp(mrb_state
*mrb
, mrb_value ary
, mrb_value
*p
, mrb_int a
, mrb_int b
, mrb_value blk
)
1592 if (mrb_nil_p(blk
)) {
1593 cmp
= mrb_cmp(mrb
, p
[a
], p
[b
]);
1594 if (cmp
== -2) cmp_failed(mrb
, a
, b
);
1597 mrb_value args
[2] = {p
[a
], p
[b
]};
1598 mrb_value c
= mrb_yield_argv(mrb
, blk
, 2, args
);
1599 if (mrb_nil_p(c
) || !mrb_fixnum_p(c
)) {
1600 cmp_failed(mrb
, a
, b
);
1602 cmp
= mrb_fixnum(c
);
1604 mrb_int size
= RARRAY_LEN(ary
);
1605 if (RARRAY_PTR(ary
) != p
|| size
< a
|| size
< b
) {
1606 mrb_raise(mrb
, E_RUNTIME_ERROR
, "array modified during sort");
1612 heapify(mrb_state
*mrb
, mrb_value ary
, mrb_value
*a
, mrb_int index
, mrb_int size
, mrb_value blk
)
1614 mrb_int max
= index
;
1615 mrb_int left_index
= 2 * index
+ 1;
1616 mrb_int right_index
= left_index
+ 1;
1617 if (left_index
< size
&& sort_cmp(mrb
, ary
, a
, left_index
, max
, blk
)) {
1620 if (right_index
< size
&& sort_cmp(mrb
, ary
, a
, right_index
, max
, blk
)) {
1624 mrb_value tmp
= a
[max
];
1627 heapify(mrb
, ary
, a
, max
, size
, blk
);
1633 * array.sort! -> self
1634 * array.sort! {|a, b| ... } -> self
1636 * Sort all elements and replace +self+ with these
1640 mrb_ary_sort_bang(mrb_state
*mrb
, mrb_value ary
)
1644 mrb_int n
= RARRAY_LEN(ary
);
1645 if (n
< 2) return ary
;
1647 ary_modify(mrb
, mrb_ary_ptr(ary
));
1648 mrb_get_args(mrb
, "&", &blk
);
1650 mrb_value
*a
= RARRAY_PTR(ary
);
1651 for (mrb_int i
= n
/ 2 - 1; i
> -1; i
--) {
1652 heapify(mrb
, ary
, a
, i
, n
, blk
);
1654 for (mrb_int i
= n
- 1; i
> 0; i
--) {
1655 mrb_value tmp
= a
[0];
1658 heapify(mrb
, ary
, a
, 0, i
, blk
);
1664 mrb_init_array(mrb_state
*mrb
)
1668 mrb
->array_class
= a
= mrb_define_class_id(mrb
, MRB_SYM(Array
), mrb
->object_class
); /* 15.2.12 */
1669 MRB_SET_INSTANCE_TT(a
, MRB_TT_ARRAY
);
1671 mrb_define_class_method_id(mrb
, a
, MRB_OPSYM(aref
), mrb_ary_s_create
, MRB_ARGS_ANY()); /* 15.2.12.4.1 */
1673 mrb_define_method_id(mrb
, a
, MRB_OPSYM(add
), mrb_ary_plus
, MRB_ARGS_REQ(1)); /* 15.2.12.5.1 */
1674 mrb_define_method_id(mrb
, a
, MRB_OPSYM(mul
), mrb_ary_times
, MRB_ARGS_REQ(1)); /* 15.2.12.5.2 */
1675 mrb_define_method_id(mrb
, a
, MRB_OPSYM(lshift
), mrb_ary_push_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.3 */
1676 mrb_define_method_id(mrb
, a
, MRB_OPSYM(aref
), mrb_ary_aget
, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.4 */
1677 mrb_define_method_id(mrb
, a
, MRB_OPSYM(aset
), mrb_ary_aset
, MRB_ARGS_ARG(2,1)); /* 15.2.12.5.5 */
1678 mrb_define_method_id(mrb
, a
, MRB_SYM(clear
), mrb_ary_clear
, MRB_ARGS_NONE()); /* 15.2.12.5.6 */
1679 mrb_define_method_id(mrb
, a
, MRB_OPSYM(cmp
), mrb_ary_cmp
, MRB_ARGS_REQ(1));
1680 mrb_define_method_id(mrb
, a
, MRB_SYM(concat
), mrb_ary_concat_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.8 */
1681 mrb_define_method_id(mrb
, a
, MRB_SYM(delete), mrb_ary_delete
, MRB_ARGS_REQ(1));
1682 mrb_define_method_id(mrb
, a
, MRB_SYM(delete_at
), mrb_ary_delete_at
, MRB_ARGS_REQ(1)); /* 15.2.12.5.9 */
1683 mrb_define_method_id(mrb
, a
, MRB_SYM_Q(empty
), mrb_ary_empty_p
, MRB_ARGS_NONE()); /* 15.2.12.5.12 */
1684 mrb_define_method_id(mrb
, a
, MRB_OPSYM(eq
), mrb_ary_eq
, MRB_ARGS_REQ(1));
1685 mrb_define_method_id(mrb
, a
, MRB_SYM_Q(eql
), mrb_ary_eql
, MRB_ARGS_REQ(1));
1686 mrb_define_method_id(mrb
, a
, MRB_SYM(first
), mrb_ary_first
, MRB_ARGS_OPT(1)); /* 15.2.12.5.13 */
1687 mrb_define_method_id(mrb
, a
, MRB_SYM(index
), mrb_ary_index_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.14 */
1688 mrb_define_method_id(mrb
, a
, MRB_SYM(initialize
), mrb_ary_init
, MRB_ARGS_OPT(2)); /* 15.2.12.5.15 */
1689 mrb_define_method_id(mrb
, a
, MRB_SYM(initialize_copy
), mrb_ary_replace_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.16 */
1690 mrb_define_method_id(mrb
, a
, MRB_SYM(join
), mrb_ary_join_m
, MRB_ARGS_OPT(1)); /* 15.2.12.5.17 */
1691 mrb_define_method_id(mrb
, a
, MRB_SYM(last
), mrb_ary_last
, MRB_ARGS_OPT(1)); /* 15.2.12.5.18 */
1692 mrb_define_method_id(mrb
, a
, MRB_SYM(length
), mrb_ary_size
, MRB_ARGS_NONE()); /* 15.2.12.5.19 */
1693 mrb_define_method_id(mrb
, a
, MRB_SYM(pop
), mrb_ary_pop
, MRB_ARGS_NONE()); /* 15.2.12.5.21 */
1694 mrb_define_method_id(mrb
, a
, MRB_SYM(push
), mrb_ary_push_m
, MRB_ARGS_ANY()); /* 15.2.12.5.22 */
1695 mrb_define_method_id(mrb
, a
, MRB_SYM(replace
), mrb_ary_replace_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.23 */
1696 mrb_define_method_id(mrb
, a
, MRB_SYM(reverse
), mrb_ary_reverse
, MRB_ARGS_NONE()); /* 15.2.12.5.24 */
1697 mrb_define_method_id(mrb
, a
, MRB_SYM_B(reverse
), mrb_ary_reverse_bang
, MRB_ARGS_NONE()); /* 15.2.12.5.25 */
1698 mrb_define_method_id(mrb
, a
, MRB_SYM(rindex
), mrb_ary_rindex_m
, MRB_ARGS_REQ(1)); /* 15.2.12.5.26 */
1699 mrb_define_method_id(mrb
, a
, MRB_SYM(shift
), mrb_ary_shift_m
, MRB_ARGS_OPT(1)); /* 15.2.12.5.27 */
1700 mrb_define_method_id(mrb
, a
, MRB_SYM(size
), mrb_ary_size
, MRB_ARGS_NONE()); /* 15.2.12.5.28 */
1701 mrb_define_method_id(mrb
, a
, MRB_SYM(slice
), mrb_ary_aget
, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.29 */
1702 mrb_define_method_id(mrb
, a
, MRB_SYM(unshift
), mrb_ary_unshift_m
, MRB_ARGS_ANY()); /* 15.2.12.5.30 */
1703 mrb_define_method_id(mrb
, a
, MRB_SYM(to_s
), mrb_ary_to_s
, MRB_ARGS_NONE());
1704 mrb_define_method_id(mrb
, a
, MRB_SYM(inspect
), mrb_ary_to_s
, MRB_ARGS_NONE());
1705 mrb_define_method_id(mrb
, a
, MRB_SYM_B(sort
), mrb_ary_sort_bang
, MRB_ARGS_NONE());
1707 mrb_define_method_id(mrb
, a
, MRB_SYM(__svalue
), mrb_ary_svalue
, MRB_ARGS_NONE());