-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
4396 lines (3951 loc) · 102 KB
/
array.c
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
/*
* MacRuby implementation of Ruby 1.9's array.c.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2007-2009, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*/
#include "ruby/ruby.h"
#include "ruby/util.h"
#include "ruby/st.h"
#include "id.h"
#include "objc.h"
#include "ruby/node.h"
#include "vm.h"
VALUE rb_cArray;
VALUE rb_cCFArray;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
VALUE rb_cNSArray0;
#endif
VALUE rb_cNSArray;
VALUE rb_cNSMutableArray;
VALUE rb_cRubyArray;
#define ARY_DEFAULT_SIZE 16
typedef struct {
struct RBasic basic;
size_t beg;
size_t len;
size_t cap;
VALUE *elements;
} rb_ary_t;
// RubyArray primitives.
static VALUE
rary_elt(rb_ary_t *ary, size_t idx)
{
// assert(idx < ary->len);
return ary->elements[ary->beg + idx];
}
static void
rary_replace(rb_ary_t *ary, size_t idx, VALUE item)
{
// assert(idx < ary->len);
GC_WB(&ary->elements[ary->beg + idx], item);
}
static VALUE *
rary_ptr(rb_ary_t *ary)
{
return &ary->elements[ary->beg];
}
static void
rary_reserve(rb_ary_t *ary, size_t newlen)
{
if (ary->beg + newlen > ary->cap) {
if (ary->beg > 0) {
if (ary->beg > newlen) {
newlen = 0;
}
else {
newlen -= ary->beg;
}
for (size_t i = 0; i < ary->len; i++) {
GC_WB(&ary->elements[i], ary->elements[ary->beg + i]);
}
ary->beg = 0;
}
if (newlen > ary->cap) {
if (ary->cap > 0) {
newlen *= 2;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
VALUE *new_elements = (VALUE *)xmalloc(sizeof(VALUE) * newlen);
for (size_t i = 0; i < ary->len; i++) {
GC_WB(&new_elements[i], ary->elements[i]);
}
GC_WB(&ary->elements, new_elements);
#else
//printf("xrealloc %p (%ld -> %ld)\n", ary, ary->cap, newlen);
VALUE *new_elements = xrealloc(ary->elements, sizeof(VALUE) * newlen);
if (new_elements != ary->elements) {
GC_WB(&ary->elements, new_elements);
}
#endif
ary->cap = newlen;
}
}
}
static void
rary_append(rb_ary_t *ary, VALUE item)
{
rary_reserve(ary, ary->len + 1);
rary_replace(ary, ary->len, item);
ary->len++;
}
static void
rary_insert(rb_ary_t *ary, size_t idx, VALUE item)
{
assert(idx <= ary->len);
if (idx < ary->len) {
rary_reserve(ary, ary->len + 1);
for (size_t i = ary->len; i > idx; i--) {
rary_replace(ary, i, rary_elt(ary, i - 1));
}
rary_replace(ary, idx, item);
ary->len++;
}
else {
rary_append(ary, item);
}
}
static VALUE
rary_erase(rb_ary_t *ary, size_t idx, size_t len)
{
assert(idx + len <= ary->len);
VALUE item = rary_elt(ary, idx);
if (idx == 0) {
for (size_t i = 0; i < len; i++) {
rary_replace(ary, i, Qnil);
}
if (len < ary->len) {
ary->beg += len;
}
else {
ary->beg = 0;
}
}
else {
for (size_t i = idx; i < ary->len - len; i++) {
rary_replace(ary, i, rary_elt(ary, i + len));
}
for (size_t i = 0; i < len; i++) {
rary_replace(ary, ary->len - i - 1, Qnil);
}
}
ary->len -= len;
return item;
}
static void
rary_store(rb_ary_t *ary, size_t idx, VALUE item)
{
if (idx >= ary->len) {
rary_reserve(ary, idx + 1);
for (size_t i = ary->len; i < idx + 1; i++) {
rary_replace(ary, i, Qnil);
}
ary->len = idx + 1;
}
rary_replace(ary, idx, item);
}
static void
rary_resize(rb_ary_t *ary, size_t newlen)
{
if (newlen > ary->cap) {
rary_reserve(ary, newlen);
}
for (size_t i = ary->len; i < newlen; i++) {
rary_replace(ary, i, Qnil);
}
ary->len = newlen;
}
static void
rary_concat(rb_ary_t *ary, rb_ary_t *other, size_t beg, size_t len)
{
rary_reserve(ary, ary->len + len);
for (size_t i = 0; i < len; i++) {
rary_replace(ary, i + ary->len, rary_elt(other, beg + i));
}
ary->len += len;
}
static void
rary_reverse(rb_ary_t *ary)
{
if (ary->len > 1) {
for (size_t i = 0; i < ary->len / 2; i++) {
const size_t j = ary->len - i - 1;
VALUE elem = rary_elt(ary, i);
rary_replace(ary, i, rary_elt(ary, j));
rary_replace(ary, j, elem);
}
}
}
static void
rary_clear(rb_ary_t *ary)
{
memset(ary->elements, 0, sizeof(VALUE) * ary->len);
ary->len = 0;
}
static VALUE
rb_equal_fast(VALUE x, VALUE y)
{
if (x == y) {
return Qtrue;
}
if (SPECIAL_CONST_P(x) && SPECIAL_CONST_P(y) && TYPE(x) == TYPE(y)) {
return Qfalse;
}
if (SYMBOL_P(x)) {
return x == y ? Qtrue : Qfalse;
}
return rb_equal(x, y);
}
#define NOT_FOUND LONG_MAX
static size_t
rary_index_of_item(rb_ary_t *ary, size_t origin, VALUE item)
{
assert(origin < ary->len);
for (size_t i = origin; i < ary->len; i++) {
VALUE item2 = rary_elt(ary, i);
if (rb_equal_fast(item, item2) == Qtrue) {
return i;
}
}
return NOT_FOUND;
}
#define IS_RARY(x) (*(VALUE *)x == rb_cRubyArray)
#define RARY(x) ((rb_ary_t *)x)
void
rb_mem_clear(register VALUE *mem, register long size)
{
while (size--) {
*mem++ = Qnil;
}
}
static inline void
__rb_ary_modify(VALUE ary)
{
long mask;
if (IS_RARY(ary)) {
mask = RBASIC(ary)->flags;
}
else {
#ifdef __LP64__
mask = RCLASS_RC_FLAGS(ary);
#else
mask = rb_objc_flag_get_mask((void *)ary);
#endif
if (RARRAY_IMMUTABLE(ary)) {
mask |= FL_FREEZE;
}
}
if ((mask & FL_FREEZE) == FL_FREEZE) {
rb_raise(rb_eRuntimeError, "can't modify frozen/immutable array");
}
if ((mask & FL_TAINT) == FL_TAINT && rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
}
}
#define rb_ary_modify(ary) \
do { \
if (!IS_RARY(ary) || RBASIC(ary)->flags != 0) { \
__rb_ary_modify(ary); \
} \
} \
while (0)
extern void _CFArraySetCapacity(CFMutableArrayRef array, CFIndex cap);
static inline void
rb_ary_set_capacity(VALUE ary, long len)
{
if (RARRAY_LEN(ary) < len) {
if (IS_RARY(ary)) {
rary_reserve(RARY(ary), len);
}
else {
_CFArraySetCapacity((CFMutableArrayRef)ary, len);
}
}
}
VALUE
rb_ary_freeze(VALUE ary)
{
return rb_obj_freeze(ary);
}
/*
* call-seq:
* array.frozen? -> true or false
*
* Return <code>true</code> if this array is frozen (or temporarily frozen
* while being sorted).
*/
VALUE
rb_ary_frozen_p(VALUE ary)
{
return OBJ_FROZEN(ary) ? Qtrue : Qfalse;
}
static VALUE
rb_ary_frozen_imp(VALUE ary, SEL sel)
{
return rb_ary_frozen_p(ary);
}
void rb_ary_insert(VALUE ary, long idx, VALUE val);
static inline VALUE
ary_alloc(VALUE klass)
{
if ((klass == 0 || klass == rb_cRubyArray || klass == rb_cNSMutableArray)
&& rb_cRubyArray != 0) {
NEWOBJ(ary, rb_ary_t);
ary->basic.flags = 0;
ary->basic.klass = rb_cRubyArray;
ary->beg = ary->len = ary->cap = 0;
ary->elements = NULL;
return (VALUE)ary;
}
else {
CFMutableArrayRef ary = CFArrayCreateMutable(NULL, 0,
&kCFTypeArrayCallBacks);
if (klass != 0 && klass != rb_cNSArray && klass != rb_cNSMutableArray) {
*(Class *)ary = (Class)klass;
}
CFMakeCollectable(ary);
return (VALUE)ary;
}
}
VALUE
rb_ary_new_fast(int argc, ...)
{
VALUE ary = ary_alloc(0);
if (argc > 0) {
va_list ar;
rary_reserve(RARY(ary), argc);
va_start(ar, argc);
for (int i = 0; i < argc; i++) {
VALUE item = va_arg(ar, VALUE);
rary_append(RARY(ary), item);
}
va_end(ar);
}
return ary;
}
static inline void
assert_ary_len(const long len)
{
if (len < 0) {
rb_raise(rb_eArgError, "negative array size (or size too big)");
}
if ((unsigned long)len > (LONG_MAX / sizeof(VALUE))) {
rb_raise(rb_eArgError, "array size too big");
}
}
static VALUE
ary_new(VALUE klass, long len)
{
assert_ary_len(len);
VALUE ary = ary_alloc(klass);
if (IS_RARY(ary)) {
rary_reserve(RARY(ary), len);
}
return ary;
}
VALUE
rb_ary_new2(long len)
{
return ary_new(0, len);
}
VALUE
rb_ary_new(void)
{
return rb_ary_new2(ARY_DEFAULT_SIZE);
}
#include <stdarg.h>
VALUE
rb_ary_new3(long n, ...)
{
VALUE ary = rb_ary_new2(n);
if (n > 0) {
va_list ar;
va_start(ar, n);
rary_reserve(RARY(ary), n);
for (long i = 0; i < n; i++) {
rary_append(RARY(ary), va_arg(ar, VALUE));
}
va_end(ar);
}
return ary;
}
VALUE
rb_ary_new4(long n, const VALUE *elts)
{
VALUE ary;
ary = rb_ary_new2(n);
if (n > 0 && elts != NULL) {
if (IS_RARY(ary)) {
for (long i = 0; i < n; i++) {
rary_append(RARY(ary), elts[i]);
}
}
else {
void **vals = (void **)alloca(n * sizeof(void *));
for (long i = 0; i < n; i++) {
vals[i] = RB2OC(elts[i]);
}
CFArrayReplaceValues((CFMutableArrayRef)ary, CFRangeMake(0, 0),
(const void **)vals, n);
}
}
return ary;
}
VALUE
rb_assoc_new(VALUE car, VALUE cdr)
{
return rb_ary_new3(2, car, cdr);
}
static VALUE
to_ary(VALUE ary)
{
return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
}
VALUE
rb_check_array_type(VALUE ary)
{
return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary");
}
long
rb_ary_len(VALUE ary)
{
if (IS_RARY(ary)) {
return RARY(ary)->len;
}
else {
return CFArrayGetCount((CFArrayRef)ary);
}
}
VALUE
rb_ary_elt(VALUE ary, long offset)
{
if (IS_RARY(ary)) {
if (offset < RARY(ary)->len) {
return rary_elt(RARY(ary), offset);
}
}
else {
if (offset < CFArrayGetCount((CFArrayRef)ary)) {
return OC2RB(CFArrayGetValueAtIndex((CFArrayRef)ary, offset));
}
}
return Qnil;
}
VALUE
rb_ary_erase(VALUE ary, long offset)
{
if (IS_RARY(ary)) {
return rary_erase(RARY(ary), offset, 1);
}
else {
VALUE item = OC2RB(CFArrayGetValueAtIndex((CFArrayRef)ary, offset));
CFArrayRemoveValueAtIndex((CFMutableArrayRef)ary, offset);
return item;
}
}
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
rb_ary_modify(ary);
if (IS_RARY(ary)) {
rary_append(RARY(ary), item);
}
else {
CFArrayAppendValue((CFMutableArrayRef)ary, (const void *)RB2OC(item));
}
return ary;
}
inline static void
rb_ary_append(VALUE ary, int argc, VALUE *argv)
{
rb_ary_modify(ary);
if (IS_RARY(ary)) {
rary_reserve(RARY(ary), argc);
for (int i = 0; i < argc; i++) {
rary_append(RARY(ary), argv[i]);
}
}
else {
for (int i = 0; i < argc; i++) {
CFArrayAppendValue((CFMutableArrayRef)ary,
(const void *)RB2OC(argv[i]));
}
}
}
static inline void
rb_ary_resize(VALUE ary, long new_len)
{
if (IS_RARY(ary)) {
rary_resize(RARY(ary), new_len);
}
else {
// TODO
abort();
}
}
/*
* call-seq:
* Array.try_convert(obj) -> array or nil
*
* Try to convert <i>obj</i> into an array, using to_ary method.
* Returns converted array or nil if <i>obj</i> cannot be converted
* for any reason. This method is to check if an argument is an
* array.
*
* Array.try_convert([1]) # => [1]
* Array.try_convert("1") # => nil
*
* if tmp = Array.try_convert(arg)
* # the argument is an array
* elsif tmp = String.try_convert(arg)
* # the argument is a string
* end
*
*/
static VALUE
rb_ary_s_try_convert(VALUE dummy, SEL sel, VALUE ary)
{
return rb_check_array_type(ary);
}
/*
* call-seq:
* Array.new(size=0, obj=nil)
* Array.new(array)
* Array.new(size) {|index| block }
*
* Returns a new array. In the first form, the new array is
* empty. In the second it is created with _size_ copies of _obj_
* (that is, _size_ references to the same
* _obj_). The third form creates a copy of the array
* passed as a parameter (the array is generated by calling
* to_ary on the parameter). In the last form, an array
* of the given size is created. Each element in this array is
* calculated by passing the element's index to the given block and
* storing the return value.
*
* Array.new
* Array.new(2)
* Array.new(5, "A")
*
* # only one copy of the object is created
* a = Array.new(2, Hash.new)
* a[0]['cat'] = 'feline'
* a
* a[1]['cat'] = 'Felix'
* a
*
* # here multiple copies are created
* a = Array.new(2) { Hash.new }
* a[0]['cat'] = 'feline'
* a
*
* squares = Array.new(5) {|i| i*i}
* squares
*
* copy = Array.new(squares)
*/
static VALUE
rb_ary_initialize(VALUE ary, SEL sel, int argc, VALUE *argv)
{
ary = (VALUE)objc_msgSend((id)ary, selInit);
if (argc == 0) {
if (rb_block_given_p()) {
rb_warning("given block not used");
}
rb_ary_clear(ary);
return ary;
}
VALUE size, val;
rb_scan_args(argc, argv, "02", &size, &val);
if (argc == 1 && !FIXNUM_P(size)) {
val = rb_check_array_type(size);
if (!NIL_P(val)) {
rb_ary_replace(ary, val);
return ary;
}
}
long len = NUM2LONG(size);
assert_ary_len(len);
rb_ary_modify(ary);
if (rb_block_given_p()) {
if (argc == 2) {
rb_warn("block supersedes default value argument");
}
rb_ary_clear(ary);
for (long i = 0; i < len; i++) {
VALUE v = rb_yield(LONG2NUM(i));
RETURN_IF_BROKEN();
rb_ary_push(ary, v);
}
}
else {
rb_ary_resize(ary, len);
for (long i = 0; i < len; i++) {
rb_ary_store(ary, i, val);
}
}
return ary;
}
/*
* Returns a new array populated with the given objects.
*
* Array.[]( 1, 'a', /^A/ )
* Array[ 1, 'a', /^A/ ]
* [ 1, 'a', /^A/ ]
*/
static VALUE
rb_ary_s_create(VALUE klass, SEL sel, int argc, VALUE *argv)
{
VALUE ary = ary_alloc(klass);
if (argc < 0) {
rb_raise(rb_eArgError, "negative array size");
}
rb_ary_append(ary, argc, argv);
return ary;
}
void
rb_ary_insert(VALUE ary, long idx, VALUE val)
{
if (idx < 0) {
idx += RARRAY_LEN(ary);
if (idx < 0) {
rb_raise(rb_eIndexError, "index %ld out of array",
idx - RARRAY_LEN(ary));
}
}
if (IS_RARY(ary)) {
if (idx > RARY(ary)->len) {
rary_resize(RARY(ary), idx + 1);
rary_store(RARY(ary), idx, val);
}
else {
rary_insert(RARY(ary), idx, val);
}
}
else {
CFArrayInsertValueAtIndex((CFMutableArrayRef)ary, idx,
(const void *)RB2OC(val));
}
}
void
rb_ary_store(VALUE ary, long idx, VALUE val)
{
if (idx < 0) {
const long len = RARRAY_LEN(ary);
idx += len;
if (idx < 0) {
rb_raise(rb_eIndexError, "index %ld out of array",
idx - len);
}
}
if (IS_RARY(ary)) {
rary_store(RARY(ary), idx, val);
}
else {
CFArraySetValueAtIndex((CFMutableArrayRef)ary, idx,
(const void *)RB2OC(val));
}
}
static VALUE
ary_shared_first(int argc, VALUE *argv, VALUE ary, bool last, bool remove)
{
VALUE nv;
rb_scan_args(argc, argv, "1", &nv);
long n = NUM2LONG(nv);
const long ary_len = RARRAY_LEN(ary);
if (n > ary_len) {
n = ary_len;
}
else if (n < 0) {
rb_raise(rb_eArgError, "negative array size");
}
long offset = 0;
if (last) {
offset = ary_len - n;
}
VALUE result = rb_ary_new();
for (long i = 0; i < n; i++) {
VALUE item = rb_ary_elt(ary, i + offset);
rary_append(RARY(result), item);
}
if (remove) {
for (long i = 0; i < n; i++) {
rb_ary_erase(ary, offset);
}
}
return result;
}
/*
* call-seq:
* array << obj -> array
*
* Append---Pushes the given object on to the end of this array. This
* expression returns the array itself, so several appends
* may be chained together.
*
* [ 1, 2 ] << "c" << "d" << [ 3, 4 ]
* #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
*
*/
static VALUE
rb_ary_push_imp(VALUE ary, SEL sel, VALUE item)
{
return rb_ary_push(ary, item);
}
/*
* call-seq:
* array.push(obj, ... ) -> array
*
* Append---Pushes the given object(s) on to the end of this array. This
* expression returns the array itself, so several appends
* may be chained together.
*
* a = [ "a", "b", "c" ]
* a.push("d", "e", "f")
* #=> ["a", "b", "c", "d", "e", "f"]
*/
static VALUE
rb_ary_push_m(VALUE ary, SEL sel, int argc, VALUE *argv)
{
if (argc == 0) {
// Even if there is nothing to push, we still need to check if the
// receiver can be modified, to conform to RubySpec.
rb_ary_modify(ary);
}
else {
while (argc--) {
rb_ary_push(ary, *argv++);
}
}
return ary;
}
VALUE
rb_ary_pop(VALUE ary)
{
rb_ary_modify(ary);
const long n = RARRAY_LEN(ary);
if (n == 0) {
return Qnil;
}
return rb_ary_erase(ary, n - 1);
}
/*
* call-seq:
* array.pop -> obj or nil
* array.pop(n) -> array
*
* Removes the last element from <i>self</i> and returns it, or
* <code>nil</code> if the array is empty.
*
* If a number _n_ is given, returns an array of the last n elements
* (or less) just like <code>array.slice!(-n, n)</code> does.
*
* a = [ "a", "b", "c", "d" ]
* a.pop #=> "d"
* a.pop(2) #=> ["b", "c"]
* a #=> ["a"]
*/
static VALUE
rb_ary_pop_m(VALUE ary, SEL sel, int argc, VALUE *argv)
{
if (argc == 0) {
return rb_ary_pop(ary);
}
rb_ary_modify(ary);
return ary_shared_first(argc, argv, ary, true, true);
}
VALUE
rb_ary_shift(VALUE ary)
{
rb_ary_modify(ary);
if (RARRAY_LEN(ary) == 0) {
return Qnil;
}
return rb_ary_erase(ary, 0);
}
/*
* call-seq:
* array.shift -> obj or nil
* array.shift(n) -> array
*
* Returns the first element of <i>self</i> and removes it (shifting all
* other elements down by one). Returns <code>nil</code> if the array
* is empty.
*
* If a number _n_ is given, returns an array of the first n elements
* (or less) just like <code>array.slice!(0, n)</code> does.
*
* args = [ "-m", "-q", "filename" ]
* args.shift #=> "-m"
* args #=> ["-q", "filename"]
*
* args = [ "-m", "-q", "filename" ]
* args.shift(2) #=> ["-m", "-q"]
* args #=> ["filename"]
*/
static VALUE
rb_ary_shift_m(VALUE ary, SEL sel, int argc, VALUE *argv)
{
if (argc == 0) {
return rb_ary_shift(ary);
}
rb_ary_modify(ary);
return ary_shared_first(argc, argv, ary, false, true);
}
/*
* call-seq:
* array.unshift(obj, ...) -> array
*
* Prepends objects to the front of <i>array</i>.
* other elements up one.
*
* a = [ "b", "c", "d" ]
* a.unshift("a") #=> ["a", "b", "c", "d"]
* a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
*/
static VALUE
rb_ary_unshift_m(VALUE ary, SEL sel, int argc, VALUE *argv)
{
rb_ary_modify(ary);
for (int i = argc - 1; i >= 0; i--) {
rb_ary_insert(ary, 0, argv[i]);
}
return ary;
}
VALUE
rb_ary_unshift(VALUE ary, VALUE item)
{
return rb_ary_unshift_m(ary, 0, 1, &item);
}
static void *rb_objc_ary_cptr_assoc_key = NULL;
const VALUE *
rb_ary_ptr(VALUE ary)
{
if (IS_RARY(ary)) {
return rary_ptr(RARY(ary));
}
const long len = RARRAY_LEN(ary);
if (len == 0) {
return NULL;
}
VALUE *values = (VALUE *)xmalloc(sizeof(VALUE) * len);
CFArrayGetValues((CFArrayRef)ary, CFRangeMake(0, len),
(const void **)values);
for (long i = 0; i < len; i++) {
values[i] = OC2RB(values[i]);
}
rb_objc_set_associative_ref((void *)ary, &rb_objc_ary_cptr_assoc_key,
values);
return values;
}
VALUE
rb_ary_entry(VALUE ary, long offset)
{
const long n = RARRAY_LEN(ary);
if (n == 0) {
return Qnil;
}
if (offset < 0) {
offset += n;
}
if (offset < 0 || n <= offset) {
return Qnil;
}
return rb_ary_elt(ary, offset);
}
VALUE
rb_ary_subseq(VALUE ary, long beg, long len)
{
if (beg < 0 || len < 0) {
return Qnil;
}
const long n = RARRAY_LEN(ary);
if (beg > n) {
return Qnil;
}
if (n < len || n < beg + len) {
len = n - beg;
}
VALUE newary = ary_alloc(rb_obj_class(ary));
if (len > 0) {
if (IS_RARY(newary)) {
if (IS_RARY(ary)) {
rary_concat(RARY(newary), RARY(ary), beg, len);
}
else {
rary_reserve(RARY(newary), len);
for (long i = 0; i < len; i++) {
VALUE item = rb_ary_elt(ary, beg + i);
rary_append(RARY(newary), item);
}
}
}
else {
void **values = (void **)alloca(sizeof(void *) * len);
CFArrayGetValues((CFArrayRef)ary, CFRangeMake(beg, len),
(const void **)values);