FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mruby/src/array.c at master · mruby/mruby · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
mruby
/
mruby
Public
Notifications
You must be signed in to change notification settings
Fork
849
Star
5.6k
Code
Issues
1
Pull requests
3
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
mruby
/
src
/
array.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2618 lines (2365 loc) · 72.7 KB
Breadcrumbs
mruby
/
src
/
array.c
Copy path
File metadata and controls
2618 lines (2365 loc) · 72.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"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))
/* Raises an ArgumentError when array size exceeds limits */
static
void
ary_too_big
(
mrb_state
*
mrb
)
{
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"array size too big"
);
}
/* Checks if array size would exceed limits and raises error if so */
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
}
/* Creates a new RArray with specified capacity */
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)
*/
/* Portable array copy function to avoid memcpy issues on some platforms */
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
];
}
}
/* Creates a new RArray initialized with values from an array */
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
);
}
/* Fills array elements with nil values */
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))
/* Prepares array for modification, handling shared arrays and frozen check */
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
);
}
/* Converts array to shared representation for copy-on-write semantics */
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
);
}
}
/* Creates a shared copy of array for temporary GC protection.
* Frozen arrays are returned as-is (cannot be modified).
* Embedded arrays get full copy (cannot be shared).
* Heap arrays get zero-copy shared reference.
*/
MRB_API
mrb_value
mrb_ary_make_shared_copy
(
mrb_state
*
mrb
,
mrb_value
ary
)
{
struct
RArray
*
orig
=
mrb_ary_ptr
(
ary
);
// Frozen arrays don't need protection
if
(
mrb_frozen_p
(
orig
)) {
return
ary
;
}
// Embedded arrays can't be shared - make full copy
if
(
ARY_EMBED_P
(
orig
)) {
return
mrb_ary_dup
(
mrb
,
ary
);
}
// Make original array shared if not already
if
(!
ARY_SHARED_P
(
orig
)) {
ary_make_shared
(
mrb
,
orig
);
}
// Create new array that shares the buffer
struct
RArray
*
shared
=
(
struct
RArray
*
)
mrb_obj_alloc
(
mrb
,
MRB_TT_ARRAY
,
mrb
->
array_class
);
shared
->
as
.
heap
.
ptr
=
orig
->
as
.
heap
.
ptr
;
shared
->
as
.
heap
.
len
=
orig
->
as
.
heap
.
len
;
shared
->
as
.
heap
.
aux
.
shared
=
orig
->
as
.
heap
.
aux
.
shared
;
shared
->
as
.
heap
.
aux
.
shared
->
refcnt
++
;
ARY_SET_SHARED_FLAG
(
shared
);
mrb_write_barrier
(
mrb
, (
struct
RBasic
*
)
shared
);
return
mrb_obj_value
(
shared
);
}
/* Expands array capacity to accommodate at least len elements */
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
;
}
}
/* Shrinks array capacity to save memory when array becomes much smaller */
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
;
}
/*
* call-seq:
* Array[obj, ...] -> new_array
*
* Creates a new Array containing the given objects:
*
* Array[1, 'a', /^A/] # => [1, "a", /^A/]
* Array[1, 2, 3] # => [1, 2, 3]
* Array[] # => []
*/
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
*
);
/*
* call-seq:
* Array.new(size=0, default=nil) -> new_array
* Array.new(array) -> new_array
* Array.new(size) {|index| ... } -> new_array
*
* Returns a new Array.
*
* With no block and no arguments, returns a new empty Array object.
*
* With no block and a single `size` argument, returns a new Array object
* of the given size whose elements are all `nil`:
*
* a = Array.new(3)
* a # => [nil, nil, nil]
* a.size # => 3
*
* With no block and arguments `size` and `default`, returns an Array object
* of the given size; each element is the same `default` object:
*
* a = Array.new(3, 'x')
* a # => ['x', 'x', 'x']
*
* With a block and argument `size`, returns an Array object 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:
*
* a = Array.new(3) {|index| "Element #{index}" }
* a # => ["Element 0", "Element 1", "Element 2"]
*
* With 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]
*/
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
);
/* Rebuilding an array through `initialize` writes over whatever it held,
and nothing below this point asks whether that is allowed: a size of
zero writes nothing at all, and the loop that fills a larger one writes
through no helper that checks. The branch above goes through
ary_replace(), which opens with the same check. */
ary_modify_check
(
mrb
,
a
);
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
;
}
/* Internal helper to concatenate two arrays */
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
;
}
/*
* call-seq:
* array + other_array -> new_array
*
* Returns a new Array containing all elements of `array`
* followed by all elements of `other_array`:
*
* a = [0, 1] + [2, 3]
* a # => [0, 1, 2, 3]
*/
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
/* Internal helper to replace array contents with another array */
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
);
}
else
{
/* Replacing an array with itself leaves it as it was, so ary_replace()
and the frozen check it opens with are skipped. The call still asks to
write, and a frozen receiver answers FrozenError for it. */
ary_modify_check
(
mrb
,
a1
);
}
}
/*
* call-seq:
* array.replace(other_array) -> self
* array.initialize_copy(other_array) -> self
*
* Replaces the contents of `self` with the contents of `other_array`;
* returns `self`:
*
* a = [0, 1, 2]
* a.replace(['foo', 'bar']) # => ["foo", "bar"]
* a # => ["foo", "bar"]
*/
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
;
}
/*
* call-seq:
* array * int -> new_array
* array * str -> new_string
*
* When the argument is an Integer `n`,
* returns a new Array built by concatenating `n` copies of `self`:
*
* a = ['x', 'y']
* a * 3 # => ["x", "y", "x", "y", "x", "y"]
*
* When the argument is a String `separator`,
* equivalent to `array.join(separator)`:
*
* [1, 2, 3] * '|' # => "1|2|3"
*/
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
);
}
/*
* call-seq:
* array.reverse! -> self
*
* Reverses `self` in place:
*
* a = ['foo', 'bar', 'two']
* a.reverse! # => ["two", "bar", "foo"]
* a # => ["two", "bar", "foo"]
*/
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
;
}
}
else
{
/* One element or none reverses into itself, so the ary_modify() above,
which is where a frozen receiver is turned away, is never reached.
The call is destructive at any length, so it is asked here. */
ary_modify_check
(
mrb
,
a
);
}
return
self
;
}
/*
* call-seq:
* array.reverse -> new_array
*
* Returns a new Array with the elements of `self` in reverse order:
*
* a = ['foo', 'bar', 'two']
* a1 = a.reverse
* a1 # => ["two", "bar", "foo"]
* a # => ["foo", "bar", "two"]
*/
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
);
}
/*
* call-seq:
* array.push(*objects) -> self
* array << object -> self
*
* Appends trailing elements.
*
* Appends each argument in `objects` to `self`; returns `self`:
*
* a = [:foo, 'bar', 2]
* a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
*
* Appends `object` to `self`; returns `self`:
*
* a = [:foo, 'bar', 2]
* a << :baz # => [:foo, "bar", 2, :baz]
*/
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
;
}
}
/*
* call-seq:
* array.shift -> object or nil
* array.shift(n) -> new_array
*
* Removes and returns leading elements.
*
* When no argument is given, removes and returns the first element:
*
* a = [:foo, 'bar', 2]
* a.shift # => :foo
* a # => ["bar", 2]
*
* Returns `nil` if `self` is empty.
*
* When argument `n` is given, removes and returns the first `n` elements in a new Array:
*
* a = [:foo, 'bar', 2]
* a.shift(2) # => [:foo, "bar"]
* a # => [2]
*/
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
;
}
MRB_API
mrb_value
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL