-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy patharray.c
More file actions
2032 lines (1805 loc) · 54.9 KB
/
array.c
File metadata and controls
2032 lines (1805 loc) · 54.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** array.c - Array class
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/string.h>
#include <mruby/range.h>
#include <mruby/proc.h>
#include <mruby/internal.h>
#include <mruby/presym.h>
#include "value_array.h"
#define ARY_DEFAULT_LEN 4
#define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
#define ARY_C_MAX_SIZE (SIZE_MAX / sizeof(mrb_value))
#ifndef MRB_ARY_LENGTH_MAX
#define MRB_ARY_LENGTH_MAX 131072
#endif
#define ARY_MAX_SIZE ((mrb_int)((ARY_C_MAX_SIZE < (size_t)MRB_INT_MAX) ? ARY_C_MAX_SIZE : MRB_INT_MAX-1))
static void
ary_too_big(mrb_state *mrb)
{
mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
}
static inline void
ary_check_too_big(mrb_state *mrb, mrb_int a, mrb_int b)
{
if (a > ARY_MAX_SIZE - b || a < 0)
ary_too_big(mrb);
#if MRB_ARY_LENGTH_MAX != 0
if (a > MRB_ARY_LENGTH_MAX - b || a < 0)
ary_too_big(mrb);
#endif
}
static struct RArray*
ary_new_capa(mrb_state *mrb, mrb_int capa)
{
ary_check_too_big(mrb, capa, 0);
size_t blen = capa * sizeof(mrb_value);
struct RArray *a = MRB_OBJ_ALLOC(mrb, MRB_TT_ARRAY, mrb->array_class);
if (capa <= MRB_ARY_EMBED_LEN_MAX) {
ARY_SET_EMBED_LEN(a, 0);
}
else {
a->as.heap.ptr = (mrb_value *)mrb_malloc(mrb, blen);
a->as.heap.aux.capa = capa;
a->as.heap.len = 0;
}
return a;
}
/**
* Creates a new array with a specified initial capacity.
*
* This function allocates an array that can hold at least `capa` elements
* without needing to immediately reallocate memory. If `capa` is 0,
* it may still allocate a small default capacity.
*
* @param mrb The mruby state.
* @param capa The initial capacity desired for the array.
* @return A new mrb_value representing the created array.
*/
MRB_API mrb_value
mrb_ary_new_capa(mrb_state *mrb, mrb_int capa)
{
struct RArray *a = ary_new_capa(mrb, capa);
return mrb_obj_value(a);
}
/**
* Creates a new, empty array.
*
* This function is equivalent to calling `mrb_ary_new_capa` with a capacity of 0.
* The array will dynamically resize as elements are added.
*
* @param mrb The mruby state.
* @return A new mrb_value representing the created empty array.
*/
MRB_API mrb_value
mrb_ary_new(mrb_state *mrb)
{
return mrb_ary_new_capa(mrb, 0);
}
/*
* To copy array, use this instead of memcpy because of portability
* * gcc on ARM may fail optimization of memcpy
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56620
* * gcc on MIPS also fail
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39755
* * memcpy doesn't exist on freestanding environment
*
* If you optimize for binary size, use memcpy instead of this at your own risk
* of above portability issue.
*
* See also https://togetter.com/li/462898 (Japanese)
*/
static inline void
array_copy(mrb_value *dst, const mrb_value *src, mrb_int size)
{
for (mrb_int i = 0; i < size; i++) {
dst[i] = src[i];
}
}
static struct RArray*
ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
{
struct RArray *a = ary_new_capa(mrb, size);
array_copy(ARY_PTR(a), vals, size);
ARY_SET_LEN(a, size);
return a;
}
/**
* Creates a new array initialized with a given sequence of values.
*
* This function allocates an array and copies `size` elements from the `vals`
* pointer into the new array.
*
* @param mrb The mruby state.
* @param size The number of values to initialize the array with.
* @param vals A pointer to an array of `mrb_value`s to copy into the new array.
* @return A new mrb_value representing the created array.
*/
MRB_API mrb_value
mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
{
struct RArray *a = ary_new_from_values(mrb, size, vals);
return mrb_obj_value(a);
}
/**
* Creates a new array of size 2, typically used to represent an association (key-value pair).
*
* The first element of the array is `car` (often the key), and the second element
* is `cdr` (often the value).
*
* @param mrb The mruby state.
* @param car The first value to be placed in the array.
* @param cdr The second value to be placed in the array.
* @return A new mrb_value representing the created 2-element array.
*/
MRB_API mrb_value
mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
{
struct RArray *a = ary_new_capa(mrb, 2);
mrb_value *p = ARY_PTR(a);
p[0] = car;
p[1] = cdr;
ARY_SET_LEN(a, 2);
return mrb_obj_value(a);
}
static void
ary_fill_with_nil(mrb_value *ptr, mrb_int size)
{
mrb_value nil = mrb_nil_value();
while (size--) {
*ptr++ = nil;
}
}
#define ary_modify_check(mrb, a) mrb_check_frozen((mrb), (a))
static void
ary_modify(mrb_state *mrb, struct RArray *a)
{
ary_modify_check(mrb, a);
if (ARY_SHARED_P(a)) {
mrb_shared_array *shared = a->as.heap.aux.shared;
if (shared->refcnt == 1 && a->as.heap.ptr == shared->ptr) {
a->as.heap.ptr = shared->ptr;
a->as.heap.aux.capa = a->as.heap.len;
mrb_free(mrb, shared);
}
else {
mrb_value *p = a->as.heap.ptr;
mrb_value *ptr = (mrb_value*)mrb_malloc(mrb, a->as.heap.len * sizeof(mrb_value));
if (p) {
array_copy(ptr, p, a->as.heap.len);
}
a->as.heap.ptr = ptr;
a->as.heap.aux.capa = a->as.heap.len;
mrb_ary_decref(mrb, shared);
}
ARY_UNSET_SHARED_FLAG(a);
}
}
/**
* Prepares an array for modification.
*
* This function ensures that the array is not frozen and is not shared.
* If the array is shared and has multiple references, this function will
* duplicate the array data to ensure that modifications do not affect
* other references. It also triggers a write barrier for the garbage collector.
*
* @param mrb The mruby state.
* @param a A pointer to the RArray structure to modify.
*/
MRB_API void
mrb_ary_modify(mrb_state *mrb, struct RArray* a)
{
mrb_write_barrier(mrb, (struct RBasic*)a);
ary_modify(mrb, a);
}
static void
ary_make_shared(mrb_state *mrb, struct RArray *a)
{
if (!ARY_SHARED_P(a) && !ARY_EMBED_P(a)) {
mrb_shared_array *shared = (mrb_shared_array*)mrb_malloc(mrb, sizeof(mrb_shared_array));
mrb_value *ptr = a->as.heap.ptr;
mrb_int len = a->as.heap.len;
shared->refcnt = 1;
if (a->as.heap.aux.capa > len) {
a->as.heap.ptr = shared->ptr = (mrb_value*)mrb_realloc(mrb, ptr, sizeof(mrb_value)*len+1);
}
else {
shared->ptr = ptr;
}
shared->len = len;
a->as.heap.aux.shared = shared;
ARY_SET_SHARED_FLAG(a);
}
}
static void
ary_expand_capa(mrb_state *mrb, struct RArray *a, mrb_int len)
{
mrb_int capa = ARY_CAPA(a);
ary_check_too_big(mrb, len, 0);
if (capa < ARY_DEFAULT_LEN) {
capa = ARY_DEFAULT_LEN;
}
while (capa < len) {
if (capa <= ARY_MAX_SIZE / 2) {
capa *= 2;
}
else {
capa = len;
}
}
if (capa > ARY_MAX_SIZE) {
ary_too_big(mrb);
}
if (ARY_EMBED_P(a)) {
mrb_value *ptr = ARY_EMBED_PTR(a);
mrb_int slen = ARY_EMBED_LEN(a);
mrb_value *expanded_ptr = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*capa);
ARY_UNSET_EMBED_FLAG(a);
array_copy(expanded_ptr, ptr, slen);
a->as.heap.len = slen;
a->as.heap.aux.capa = capa;
a->as.heap.ptr = expanded_ptr;
}
else if (capa > a->as.heap.aux.capa) {
mrb_value *expanded_ptr = (mrb_value*)mrb_realloc(mrb, a->as.heap.ptr, sizeof(mrb_value)*capa);
a->as.heap.aux.capa = capa;
a->as.heap.ptr = expanded_ptr;
}
}
static void
ary_shrink_capa(mrb_state *mrb, struct RArray *a)
{
if (ARY_EMBED_P(a)) return;
mrb_int capa = a->as.heap.aux.capa;
if (capa < ARY_DEFAULT_LEN * 2) return;
if (capa <= a->as.heap.len * ARY_SHRINK_RATIO) return;
do {
capa /= 2;
if (capa < ARY_DEFAULT_LEN) {
capa = ARY_DEFAULT_LEN;
break;
}
} while (capa > a->as.heap.len * ARY_SHRINK_RATIO);
if (capa > a->as.heap.len && capa < a->as.heap.aux.capa) {
a->as.heap.aux.capa = capa;
a->as.heap.ptr = (mrb_value*)mrb_realloc(mrb, a->as.heap.ptr, sizeof(mrb_value)*capa);
}
}
/**
* Resizes an array to a new length.
*
* If `new_len` is smaller than the current length, the array is truncated.
* If `new_len` is larger than the current length, the array is expanded,
* and new elements are filled with `nil`.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param ary The array (mrb_value) to resize.
* @param new_len The desired new length of the array.
* @return The resized array (the same mrb_value as `ary`).
*/
MRB_API mrb_value
mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len)
{
struct RArray *a = mrb_ary_ptr(ary);
ary_modify(mrb, a);
mrb_int old_len = RARRAY_LEN(ary);
if (old_len != new_len) {
if (new_len < old_len) {
ary_shrink_capa(mrb, a);
}
else {
ary_expand_capa(mrb, a, new_len);
ary_fill_with_nil(ARY_PTR(a) + old_len, new_len - old_len);
}
ARY_SET_LEN(a, new_len);
}
return ary;
}
static mrb_value
mrb_ary_s_create(mrb_state *mrb, mrb_value klass)
{
const mrb_value *vals;
mrb_int len;
mrb_get_args(mrb, "*!", &vals, &len);
mrb_value ary = mrb_ary_new_from_values(mrb, len, vals);
struct RArray *a = mrb_ary_ptr(ary);
a->c = mrb_class_ptr(klass);
return ary;
}
static void ary_replace(mrb_state*, struct RArray*, struct RArray*);
static mrb_value
mrb_ary_init(mrb_state *mrb, mrb_value ary)
{
mrb_value ss = mrb_fixnum_value(0);
mrb_value obj = mrb_nil_value();
mrb_value blk = mrb_nil_value();
mrb_get_args(mrb, "|oo&", &ss, &obj, &blk);
if (mrb_array_p(ss) && mrb_nil_p(obj) && mrb_nil_p(blk)) {
ary_replace(mrb, mrb_ary_ptr(ary), mrb_ary_ptr(ss));
return ary;
}
mrb_int size = mrb_as_int(mrb, ss);
struct RArray *a = mrb_ary_ptr(ary);
if (ARY_CAPA(a) < size) {
ary_expand_capa(mrb, a, size);
}
int ai = mrb_gc_arena_save(mrb);
for (mrb_int i=0; i<size; i++) {
mrb_value val;
if (mrb_nil_p(blk)) {
val = obj;
}
else {
val = mrb_yield(mrb, blk, mrb_fixnum_value(i));
}
mrb_ary_set(mrb, ary, i, val);
mrb_gc_arena_restore(mrb, ai); // for mrb_funcall
}
return ary;
}
static void
ary_concat(mrb_state *mrb, struct RArray *a, struct RArray *a2)
{
mrb_int len = ARY_LEN(a);
if (len == 0) {
ary_replace(mrb, a, a2);
return;
}
mrb_int len2 = ARY_LEN(a2);
ary_check_too_big(mrb, len2, len);
ary_modify(mrb, a);
mrb_int newlen = len + len2;
if (ARY_CAPA(a) < newlen) {
ary_expand_capa(mrb, a, newlen);
}
array_copy(ARY_PTR(a)+len, ARY_PTR(a2), len2);
mrb_write_barrier(mrb, (struct RBasic*)a);
ARY_SET_LEN(a, newlen);
}
/**
* Concatenates one array to another.
*
* Appends all elements from the `other` array to the `self` array.
* This function modifies the `self` array in place.
*
* @param mrb The mruby state.
* @param self The array (mrb_value) to which elements will be added.
* @param other The array (mrb_value) whose elements will be appended.
*/
MRB_API void
mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
{
struct RArray *a2 = mrb_ary_ptr(other);
ary_concat(mrb, mrb_ary_ptr(self), a2);
}
/*
* call-seq:
* array.concat(*other_arrays) -> 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]
*/
static mrb_value
mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
{
mrb_value *args;
mrb_int len;
mrb_get_args(mrb, "*!", &args, &len);
for (int i=0; i<len; i++) {
mrb_ensure_array_type(mrb, args[i]);
}
for (int i=0; i<len; i++) {
mrb_ary_concat(mrb, self, args[i]);
}
return self;
}
static mrb_value
mrb_ary_plus(mrb_state *mrb, mrb_value self)
{
struct RArray *a1 = mrb_ary_ptr(self);
const mrb_value *ptr;
mrb_int blen;
mrb_get_args(mrb, "a", &ptr, &blen);
ary_check_too_big(mrb, ARY_LEN(a1), blen);
mrb_int len1 = ARY_LEN(a1);
struct RArray *a2 = ary_new_capa(mrb, len1 + blen);
array_copy(ARY_PTR(a2), ARY_PTR(a1), len1);
array_copy(ARY_PTR(a2) + len1, ptr, blen);
ARY_SET_LEN(a2, len1+blen);
return mrb_obj_value(a2);
}
#define ARY_REPLACE_SHARED_MIN 20
static void
ary_replace(mrb_state *mrb, struct RArray *a, struct RArray *b)
{
mrb_int len = ARY_LEN(b);
ary_modify_check(mrb, a);
if (a == b) return;
if (ARY_SHARED_P(a)) {
mrb_ary_decref(mrb, a->as.heap.aux.shared);
a->as.heap.aux.capa = 0;
a->as.heap.len = 0;
a->as.heap.ptr = NULL;
ARY_UNSET_SHARED_FLAG(a);
}
if (ARY_SHARED_P(b)) {
shared_b:
if (ARY_EMBED_P(a)) {
ARY_UNSET_EMBED_FLAG(a);
}
else {
mrb_free(mrb, a->as.heap.ptr);
}
a->as.heap.ptr = b->as.heap.ptr;
a->as.heap.len = len;
a->as.heap.aux.shared = b->as.heap.aux.shared;
a->as.heap.aux.shared->refcnt++;
ARY_SET_SHARED_FLAG(a);
mrb_write_barrier(mrb, (struct RBasic*)a);
return;
}
if (len > ARY_REPLACE_SHARED_MIN) {
ary_make_shared(mrb, b);
goto shared_b;
}
if (ARY_CAPA(a) < len)
ary_expand_capa(mrb, a, len);
array_copy(ARY_PTR(a), ARY_PTR(b), len);
mrb_write_barrier(mrb, (struct RBasic*)a);
ARY_SET_LEN(a, len);
}
/**
* Replaces the contents of an array with the contents of another array.
*
* After this operation, the `self` array will contain the same elements
* as the `other` array. This function modifies the `self` array in place.
*
* @param mrb The mruby state.
* @param self The array (mrb_value) whose contents will be replaced.
* @param other The array (mrb_value) from which to copy the elements.
*/
MRB_API void
mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other)
{
struct RArray *a1 = mrb_ary_ptr(self);
struct RArray *a2 = mrb_ary_ptr(other);
if (a1 != a2) {
ary_replace(mrb, a1, a2);
}
}
static mrb_value
mrb_ary_replace_m(mrb_state *mrb, mrb_value self)
{
mrb_value other;
mrb_get_args(mrb, "A", &other);
mrb_ary_replace(mrb, self, other);
return self;
}
static mrb_value
mrb_ary_times(mrb_state *mrb, mrb_value self)
{
struct RArray *a1 = mrb_ary_ptr(self);
mrb_value arg = mrb_get_arg1(mrb);
mrb_value tmp = mrb_check_string_type(mrb, arg);
if (!mrb_nil_p(tmp)) {
return mrb_ary_join(mrb, self, tmp);
}
mrb_int times = mrb_as_int(mrb, arg);
if (times < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
}
if (times == 0) return mrb_ary_new(mrb);
if (ARY_MAX_SIZE / times < ARY_LEN(a1)) {
ary_too_big(mrb);
}
mrb_int len1 = ARY_LEN(a1);
struct RArray *a2 = ary_new_capa(mrb, len1 * times);
ARY_SET_LEN(a2, len1 * times);
mrb_value *ptr = ARY_PTR(a2);
while (times--) {
array_copy(ptr, ARY_PTR(a1), len1);
ptr += len1;
}
return mrb_obj_value(a2);
}
static mrb_value
mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
mrb_int len = ARY_LEN(a);
if (len > 1) {
ary_modify(mrb, a);
mrb_value *p1 = ARY_PTR(a);
mrb_value *p2 = p1 + len - 1;
while (p1 < p2) {
mrb_value tmp = *p1;
*p1++ = *p2;
*p2-- = tmp;
}
}
return self;
}
static mrb_value
mrb_ary_reverse(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self), *b = ary_new_capa(mrb, ARY_LEN(a));
mrb_int len = ARY_LEN(a);
if (len > 0) {
mrb_value *p1 = ARY_PTR(a);
mrb_value *e = p1 + len;
mrb_value *p2 = ARY_PTR(b) + len - 1;
while (p1 < e) {
*p2-- = *p1++;
}
ARY_SET_LEN(b, len);
}
return mrb_obj_value(b);
}
/**
* Pushes an element onto the end of an array.
*
* This function appends `elem` to the `ary` array, increasing its length by one.
* The array capacity may be expanded if necessary.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param ary The array (mrb_value) to push the element onto.
* @param elem The mrb_value to append to the array.
*/
MRB_API void
mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem)
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int len = ARY_LEN(a);
ary_modify(mrb, a);
if (len == ARY_CAPA(a))
ary_expand_capa(mrb, a, len + 1);
ARY_PTR(a)[len] = elem;
ARY_SET_LEN(a, len+1);
mrb_field_write_barrier_value(mrb, (struct RBasic*)a, elem);
}
static mrb_value
mrb_ary_push_m(mrb_state *mrb, mrb_value self)
{
mrb_int argc = mrb_get_argc(mrb);
if (argc == 1) {
mrb_ary_push(mrb, self, mrb_get_argv(mrb)[0]);
return self;
}
struct RArray *a = mrb_ary_ptr(self);
mrb_int len = ARY_LEN(a);
mrb_int len2 = len + argc;
ary_modify(mrb, a);
if (ARY_CAPA(a) < len2) {
ary_expand_capa(mrb, a, len2);
}
const mrb_value *argv = mrb_get_argv(mrb);
array_copy(ARY_PTR(a)+len, argv, argc);
ARY_SET_LEN(a, len2);
while (argc--) {
mrb_field_write_barrier_value(mrb, (struct RBasic*)a, *argv);
argv++;
}
return self;
}
/**
* Removes and returns the last element from an array.
*
* If the array is empty, returns `nil`.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param ary The array (mrb_value) from which to pop the element.
* @return The last element of the array, or `nil` if the array is empty.
*/
MRB_API mrb_value
mrb_ary_pop(mrb_state *mrb, mrb_value ary)
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int len = ARY_LEN(a);
ary_modify_check(mrb, a);
if (len == 0) return mrb_nil_value();
ARY_SET_LEN(a, len-1);
return ARY_PTR(a)[len-1];
}
#define ARY_SHIFT_SHARED_MIN 10
/**
* Removes and returns the first element from an array.
*
* If the array is empty, returns `nil`.
* All other elements are shifted down by one index.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param self The array (mrb_value) from which to shift the element.
* @return The first element of the array, or `nil` if the array is empty.
*/
MRB_API mrb_value
mrb_ary_shift(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
mrb_int len = ARY_LEN(a);
ary_modify_check(mrb, a);
if (len == 0) return mrb_nil_value();
if (ARY_SHARED_P(a)) {
L_SHIFT:
a->as.heap.ptr++;
a->as.heap.len--;
return a->as.heap.ptr[-1];
}
else if (len > ARY_SHIFT_SHARED_MIN) {
ary_make_shared(mrb, a);
goto L_SHIFT;
}
else {
mrb_value *ptr = ARY_PTR(a);
mrb_int size = len;
mrb_value val = *ptr;
while (--size) {
*ptr = *(ptr+1);
ptr++;
}
ARY_SET_LEN(a, len-1);
return val;
}
}
static mrb_value
mrb_ary_shift_m(mrb_state *mrb, mrb_value self)
{
if (mrb_get_argc(mrb) == 0) {
return mrb_ary_shift(mrb, self);
}
mrb_int n = mrb_as_int(mrb, mrb_get_arg1(mrb));
struct RArray *a = mrb_ary_ptr(self);
mrb_int len = ARY_LEN(a);
ary_modify_check(mrb, a);
if (len == 0 || n == 0) return mrb_ary_new(mrb);
if (n < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array shift");
if (n > len) n = len;
mrb_value val = mrb_ary_new_from_values(mrb, n, ARY_PTR(a));
if (ARY_SHARED_P(a)) {
L_SHIFT:
a->as.heap.ptr+=n;
a->as.heap.len-=n;
return val;
}
if (len > ARY_SHIFT_SHARED_MIN) {
ary_make_shared(mrb, a);
goto L_SHIFT;
}
else if (len == n) {
ARY_SET_LEN(a, 0);
}
else {
mrb_value *ptr = ARY_PTR(a);
mrb_int size = len-n;
while (size--) {
*ptr = *(ptr+n);
ptr++;
}
ARY_SET_LEN(a, len-n);
}
return val;
}
/* self = [1,2,3]
item = 0
self.unshift item
p self #=> [0, 1, 2, 3] */
/**
* Prepends an element to the beginning of an array.
*
* This function adds `item` to the front of the `self` array,
* shifting all existing elements up by one index.
* The array capacity may be expanded if necessary.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param self The array (mrb_value) to unshift the element onto.
* @param item The mrb_value to prepend to the array.
* @return The modified array (the same mrb_value as `self`).
*/
MRB_API mrb_value
mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
{
struct RArray *a = mrb_ary_ptr(self);
mrb_int len = ARY_LEN(a);
if (ARY_SHARED_P(a)
&& a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */
&& a->as.heap.ptr - a->as.heap.aux.shared->ptr >= 1) /* there's room for unshifted item */ {
a->as.heap.ptr--;
a->as.heap.ptr[0] = item;
}
else {
mrb_value *ptr;
ary_modify(mrb, a);
if (ARY_CAPA(a) < len + 1)
ary_expand_capa(mrb, a, len + 1);
ptr = ARY_PTR(a);
value_move(ptr + 1, ptr, len);
ptr[0] = item;
}
ARY_SET_LEN(a, len+1);
mrb_field_write_barrier_value(mrb, (struct RBasic*)a, item);
return self;
}
/*
* call-seq:
* array.unshift(*objects) -> self
*
* Prepends the given +objects+ to +self+:
*
* 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 mrb_value
mrb_ary_unshift_m(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
mrb_value *ptr;
mrb_int alen = mrb_get_argc(mrb);
if (alen == 0) {
ary_modify_check(mrb, a);
return self;
}
const mrb_value *vals = mrb_get_argv(mrb);
mrb_int len = ARY_LEN(a);
if (alen > ARY_MAX_SIZE - len) {
ary_too_big(mrb);
}
if (ARY_SHARED_P(a)
&& a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */
&& a->as.heap.ptr - a->as.heap.aux.shared->ptr >= alen) /* there's room for unshifted item */ {
ary_modify_check(mrb, a);
a->as.heap.ptr -= alen;
ptr = a->as.heap.ptr;
}
else {
mrb_bool same = vals == ARY_PTR(a);
ary_modify(mrb, a);
if (ARY_CAPA(a) < len + alen)
ary_expand_capa(mrb, a, len + alen);
ptr = ARY_PTR(a);
value_move(ptr + alen, ptr, len);
if (same) vals = ptr;
}
array_copy(ptr, vals, alen);
ARY_SET_LEN(a, len+alen);
while (alen--) {
mrb_field_write_barrier_value(mrb, (struct RBasic*)a, vals[alen]);
}
return self;
}
/**
* Sets the element at a given index in an array.
*
* If `n` is within the current bounds of the array, the element at that index
* is replaced with `val`.
* If `n` is beyond the current bounds, the array is expanded to accommodate
* the new element, and any intermediate elements are filled with `nil`.
* If `n` is negative, it counts from the end of the array.
* An IndexError is raised if a negative index points past the beginning of the array.
* This function modifies the array in place.
*
* @param mrb The mruby state.
* @param ary The array (mrb_value) to modify.
* @param n The index at which to set the element.
* @param val The mrb_value to set at the specified index.
*/
MRB_API void
mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int len = ARY_LEN(a);
ary_modify(mrb, a);
/* range check */
if (n < 0) {
n += len;
if (n < 0) {
mrb_raisef(mrb, E_INDEX_ERROR, "index %i out of array", n - len);
}
}
if (n >= ARY_MAX_SIZE) {
mrb_raise(mrb, E_INDEX_ERROR, "index too big");
}
if (len <= n) {
if (ARY_CAPA(a) <= n)
ary_expand_capa(mrb, a, n + 1);
ary_fill_with_nil(ARY_PTR(a) + len, n + 1 - len);
ARY_SET_LEN(a, n+1);
}
ARY_PTR(a)[n] = val;
mrb_field_write_barrier_value(mrb, (struct RBasic*)a, val);
}
static struct RArray*
ary_dup(mrb_state *mrb, struct RArray *a)
{
return ary_new_from_values(mrb, ARY_LEN(a), ARY_PTR(a));
}
MRB_API mrb_value
mrb_ary_dup(mrb_state *mrb, mrb_value ary)
{
return mrb_obj_value(ary_dup(mrb, mrb_ary_ptr(ary)));
}
/**
* Replaces a portion of an array with elements from another array or a single value.
*
* Removes `len` elements from `ary` starting at `head` index, and inserts
* the elements from `rpl` (if `rpl` is an array) or `rpl` itself (if it's not an array)
* at that position.
* If `head` is negative, it counts from the end of the array.
* If `len` is negative, an IndexError is raised.
* If `rpl` is `mrb_undef_p()`, then the elements are removed without replacement.
* This function modifies the `ary` array in place.
*
* @param mrb The mruby state.
* @param ary The array (mrb_value) to modify.
* @param head The starting index for the splice operation.
* @param len The number of elements to remove.
* @param rpl The mrb_value to insert (can be an array or a single value, or mrb_undef_p()).
* @return The modified array (the same mrb_value as `ary`).
*/
MRB_API mrb_value
mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl)
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int alen = ARY_LEN(a);
const mrb_value *argv;
mrb_int argc;
mrb_int tail;
ary_modify(mrb, a);
/* len check */
if (len < 0) mrb_raisef(mrb, E_INDEX_ERROR, "negative length (%i)", len);
/* range check */
if (head < 0) {
head += alen;
if (head < 0) goto out_of_range;
}
if (head > ARY_MAX_SIZE - len) {
out_of_range:
mrb_raisef(mrb, E_INDEX_ERROR, "index %i is out of array", head);
}
tail = head + len;
if (alen < len || alen < tail) {
len = alen - head;
tail = head + len;
}
/* size check */
if (mrb_array_p(rpl)) {
argc = RARRAY_LEN(rpl);
argv = RARRAY_PTR(rpl);
if (argv == ARY_PTR(a)) {
struct RArray *r;
if (argc > 32767) {