-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
executable file
·3357 lines (2954 loc) · 79.6 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
/**********************************************************************
array.c -
$Author: akr $
$Date: 2007-12-24 17:19:28 +0900 (Mon, 24 Dec 2007) $
created at: Fri Aug 6 09:46:12 JST 1993
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 "yard/include/yard.h"
VALUE rb_cArray;
static ID id_cmp;
#define ARY_DEFAULT_SIZE 16
void
rb_mem_clear(register VALUE *mem, register long size)
{
while (size--) {
*mem++ = Qnil;
}
}
static inline void
memfill(register VALUE *mem, register long size, register VALUE val)
{
while (size--) {
*mem++ = val;
}
}
#define ARY_ITERLOCK FL_USER1
static void
ary_iter_check(VALUE ary)
{
if (FL_TEST(ary, ARY_ITERLOCK)) {
rb_raise(rb_eRuntimeError, "can't modify array during iteration");
}
}
#define ARY_SORTLOCK FL_USER3
#define ARY_SHARED_P(a) FL_TEST(a, ELTS_SHARED)
#define ARY_SET_LEN(ary, n) do { \
RARRAY(ary)->len = (n);\
} while (0)
#define ARY_CAPA(ary) RARRAY(ary)->aux.capa
#define RESIZE_CAPA(ary,capacity) do {\
REALLOC_N(RARRAY(ary)->ptr, VALUE, (capacity));\
RARRAY(ary)->aux.capa = (capacity);\
} while (0)
#define ITERATE(func, ary) do { \
FL_SET(ary, ARY_ITERLOCK); \
return rb_ensure(func, (ary), each_unlock, (ary));\
} while (0)
static inline void
rb_ary_modify_check(VALUE ary)
{
if (OBJ_FROZEN(ary)) rb_error_frozen("array");
if (FL_TEST(ary, ARY_SORTLOCK))
rb_raise(rb_eRuntimeError, "can't modify array during sort");
if (!OBJ_TAINTED(ary) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
}
static void
rb_ary_modify(VALUE ary)
{
VALUE *ptr;
rb_ary_modify_check(ary);
if (ARY_SHARED_P(ary)) {
ptr = ALLOC_N(VALUE, RARRAY_LEN(ary));
FL_UNSET(ary, ELTS_SHARED);
RARRAY(ary)->aux.capa = RARRAY_LEN(ary);
MEMCPY(ptr, RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
RARRAY(ary)->ptr = ptr;
}
}
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).
*/
static VALUE
rb_ary_frozen_p(VALUE ary)
{
if (OBJ_FROZEN(ary)) return Qtrue;
if (FL_TEST(ary, ARY_SORTLOCK)) return Qtrue;
return Qfalse;
}
static VALUE
ary_alloc(VALUE klass)
{
NEWOBJ(ary, struct RArray);
OBJSETUP(ary, klass, T_ARRAY);
ary->len = 0;
ary->ptr = 0;
ary->aux.capa = 0;
return (VALUE)ary;
}
static VALUE
ary_new(VALUE klass, long len)
{
VALUE ary;
if (len < 0) {
rb_raise(rb_eArgError, "negative array size (or size too big)");
}
if (len > 0 && len * sizeof(VALUE) <= len) {
rb_raise(rb_eArgError, "array size too big");
}
ary = ary_alloc(klass);
if (len == 0) len++;
RARRAY(ary)->ptr = ALLOC_N(VALUE, len);
RARRAY(ary)->aux.capa = len;
return ary;
}
VALUE
rb_ary_new2(long len)
{
return ary_new(rb_cArray, len);
}
VALUE
rb_ary_new(void)
{
return rb_ary_new2(ARY_DEFAULT_SIZE);
}
#include <stdarg.h>
VALUE
rb_ary_new3(long n, ...)
{
va_list ar;
VALUE ary;
long i;
ary = rb_ary_new2(n);
va_start(ar, n);
for (i=0; i<n; i++) {
RARRAY_PTR(ary)[i] = va_arg(ar, VALUE);
}
va_end(ar);
RARRAY(ary)->len = n;
return ary;
}
VALUE
rb_ary_new4(long n, const VALUE *elts)
{
VALUE ary;
ary = rb_ary_new2(n);
if (n > 0 && elts) {
MEMCPY(RARRAY_PTR(ary), elts, VALUE, n);
RARRAY(ary)->len = n;
}
return ary;
}
void
rb_ary_free(VALUE ary)
{
if (!ARY_SHARED_P(ary)) {
xfree(RARRAY(ary)->ptr);
}
}
static VALUE
ary_make_shared(VALUE ary)
{
if (ARY_SHARED_P(ary)) {
return RARRAY(ary)->aux.shared;
}
else {
NEWOBJ(shared, struct RArray);
OBJSETUP(shared, 0, T_ARRAY);
shared->len = RARRAY(ary)->len;
shared->ptr = RARRAY(ary)->ptr;
shared->aux.capa = RARRAY(ary)->aux.capa;
RARRAY(ary)->aux.shared = (VALUE)shared;
FL_SET(ary, ELTS_SHARED);
OBJ_FREEZE(shared);
return (VALUE)shared;
}
}
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");
}
/*
* 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, 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(int argc, VALUE *argv, VALUE ary)
{
long len;
VALUE size, val;
rb_ary_modify(ary);
ary_iter_check(ary);
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
if (RARRAY_PTR(ary) && !ARY_SHARED_P(ary)) {
free(RARRAY(ary)->ptr);
}
RARRAY(ary)->len = 0;
if (rb_block_given_p()) {
rb_warning("given block not used");
}
return ary;
}
if (argc == 1 && !FIXNUM_P(size)) {
val = rb_check_array_type(size);
if (!NIL_P(val)) {
rb_ary_replace(ary, val);
return ary;
}
}
len = NUM2LONG(size);
if (len < 0) {
rb_raise(rb_eArgError, "negative array size");
}
if (len > 0 && len * (long)sizeof(VALUE) <= len) {
rb_raise(rb_eArgError, "array size too big");
}
rb_ary_modify(ary);
RESIZE_CAPA(ary, len);
if (rb_block_given_p()) {
long i;
if (argc == 2) {
rb_warn("block supersedes default value argument");
}
for (i=0; i<len; i++) {
rb_ary_store(ary, i, rb_yield(LONG2NUM(i)));
RARRAY(ary)->len = i + 1;
}
}
else {
memfill(RARRAY_PTR(ary), len, val);
RARRAY(ary)->len = len;
}
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(int argc, VALUE *argv, VALUE klass)
{
VALUE ary = ary_alloc(klass);
if (argc < 0) {
rb_raise(rb_eArgError, "negative array size");
}
RARRAY(ary)->ptr = ALLOC_N(VALUE, argc);
RARRAY(ary)->aux.capa = argc;
MEMCPY(RARRAY_PTR(ary), argv, VALUE, argc);
RARRAY(ary)->len = argc;
return ary;
}
void
rb_ary_store(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));
}
}
rb_ary_modify(ary);
if (idx >= ARY_CAPA(ary)) {
long new_capa = ARY_CAPA(ary) / 2;
if (new_capa < ARY_DEFAULT_SIZE) {
new_capa = ARY_DEFAULT_SIZE;
}
if (new_capa + idx < new_capa) {
rb_raise(rb_eArgError, "index too big");
}
new_capa += idx;
if (new_capa * (long)sizeof(VALUE) <= new_capa) {
rb_raise(rb_eArgError, "index too big");
}
RESIZE_CAPA(ary, new_capa);
}
if (idx > RARRAY_LEN(ary)) {
rb_mem_clear(RARRAY_PTR(ary) + RARRAY_LEN(ary), idx-RARRAY_LEN(ary) + 1);
}
if (idx >= RARRAY_LEN(ary)) {
RARRAY(ary)->len = idx + 1;
}
RARRAY_PTR(ary)[idx] = val;
yard_object_modification(ary, val, YARD_OBJECT_MODIFICATION, idx);
}
static VALUE
ary_shared_array(VALUE klass, VALUE ary)
{
VALUE val = ary_alloc(klass);
ary_make_shared(ary);
RARRAY(val)->ptr = RARRAY(ary)->ptr;
RARRAY(val)->len = RARRAY(ary)->len;
RARRAY(val)->aux.shared = RARRAY(ary)->aux.shared;
FL_SET(val, ELTS_SHARED);
return val;
}
static VALUE
ary_shared_first(int argc, VALUE *argv, VALUE ary, int last)
{
VALUE nv, result;
long n;
long offset = 0;
rb_scan_args(argc, argv, "1", &nv);
n = NUM2LONG(nv);
if (n > RARRAY_LEN(ary)) {
n = RARRAY_LEN(ary);
}
else if (n < 0) {
rb_raise(rb_eArgError, "negative array size");
}
if (last) {
offset = RARRAY_LEN(ary) - n;
}
result = ary_shared_array(rb_cArray, ary);
RARRAY(result)->ptr += offset;
RARRAY(result)->len = n;
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 ] ]
*
*/
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
rb_ary_store(ary, RARRAY_LEN(ary), item);
return ary;
}
/*
* 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(int argc, VALUE *argv, VALUE ary)
{
while (argc--) {
rb_ary_push(ary, *argv++);
}
return ary;
}
VALUE
rb_ary_pop(VALUE ary)
{
long n;
rb_ary_modify_check(ary);
if (RARRAY_LEN(ary) == 0) return Qnil;
if (!ARY_SHARED_P(ary) &&
RARRAY_LEN(ary) * 3 < ARY_CAPA(ary) &&
ARY_CAPA(ary) > ARY_DEFAULT_SIZE)
{
RESIZE_CAPA(ary, RARRAY_LEN(ary) * 2);
}
n = RARRAY_LEN(ary)-1;
RARRAY(ary)->len = n;
return yard_resolve_stub(RARRAY_PTR(ary)[n]);
}
/*
* call-seq:
* array.pop -> obj or nil
*
* Removes the last element from <i>self</i> and returns it, or
* <code>nil</code> if the array is empty.
*
* a = [ "a", "b", "c", "d" ]
* a.pop #=> "d"
* a.pop(2) #=> ["b", "c"]
* a #=> ["a"]
*/
static VALUE
rb_ary_pop_m(int argc, VALUE *argv, VALUE ary)
{
VALUE result;
if (argc == 0) {
return rb_ary_pop(ary);
}
rb_ary_modify_check(ary);
result = ary_shared_first(argc, argv, ary, Qtrue);
RARRAY(ary)->len -= RARRAY_LEN(result);
return result;
}
VALUE
rb_ary_shift(VALUE ary)
{
VALUE top;
rb_ary_modify_check(ary);
ary_iter_check(ary);
if (RARRAY_LEN(ary) == 0) return Qnil;
top = yard_resolve_stub(RARRAY_PTR(ary)[0]);
if (!ARY_SHARED_P(ary)) {
if (RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) {
MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+1, VALUE, RARRAY_LEN(ary)-1);
RARRAY(ary)->len--;
return top;
}
RARRAY_PTR(ary)[0] = Qnil;
ary_make_shared(ary);
}
RARRAY(ary)->ptr++; /* shift ptr */
RARRAY(ary)->len--;
return top;
}
/*
* call-seq:
* array.shift -> obj or nil
*
* 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.
*
* 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(int argc, VALUE *argv, VALUE ary)
{
VALUE result;
long n;
if (argc == 0) {
return rb_ary_shift(ary);
}
rb_ary_modify_check(ary);
ary_iter_check(ary);
result = ary_shared_first(argc, argv, ary, Qfalse);
n = RARRAY_LEN(result);
if (ARY_SHARED_P(ary)) {
RARRAY(ary)->ptr += n;
RARRAY(ary)->len -= n;
}
else {
MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+n, VALUE, RARRAY_LEN(ary)-n);
RARRAY(ary)->len -= n;
}
return result;
}
/*
* 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(int argc, VALUE *argv, VALUE ary)
{
long len = RARRAY(ary)->len;
if (argc == 0) return ary;
rb_ary_modify(ary);
ary_iter_check(ary);
if (RARRAY(ary)->aux.capa <= RARRAY_LEN(ary)+argc) {
RESIZE_CAPA(ary, RARRAY(ary)->aux.capa + ARY_DEFAULT_SIZE);
}
/* sliding items */
MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
RARRAY(ary)->len += argc;
return ary;
}
VALUE
rb_ary_unshift(VALUE ary, VALUE item)
{
return rb_ary_unshift_m(1,&item,ary);
}
/* faster version - use this if you don't need to treat negative offset */
static inline VALUE
rb_ary_elt(VALUE ary, long offset)
{
if (RARRAY_LEN(ary) == 0) return Qnil;
if (offset < 0 || RARRAY_LEN(ary) <= offset) {
return Qnil;
}
return yard_resolve_stub(RARRAY_PTR(ary)[offset]);
}
VALUE
rb_ary_entry(VALUE ary, long offset)
{
if (offset < 0) {
offset += RARRAY_LEN(ary);
}
return rb_ary_elt(ary, offset);
}
VALUE
rb_ary_subseq(VALUE ary, long beg, long len)
{
VALUE klass, ary2, shared;
VALUE *ptr;
if (beg > RARRAY_LEN(ary)) return Qnil;
if (beg < 0 || len < 0) return Qnil;
if (RARRAY_LEN(ary) < len || RARRAY_LEN(ary) < beg + len) {
len = RARRAY_LEN(ary) - beg;
}
klass = rb_obj_class(ary);
if (len == 0) return ary_new(klass, 0);
shared = ary_make_shared(ary);
ptr = RARRAY_PTR(ary);
ary2 = ary_alloc(klass);
RARRAY(ary2)->ptr = ptr + beg;
RARRAY(ary2)->len = len;
RARRAY(ary2)->aux.shared = shared;
FL_SET(ary2, ELTS_SHARED);
return ary2;
}
/*
* call-seq:
* array[index] -> obj or nil
* array[start, length] -> an_array or nil
* array[range] -> an_array or nil
* array.slice(index) -> obj or nil
* array.slice(start, length) -> an_array or nil
* array.slice(range) -> an_array or nil
*
* Element Reference---Returns the element at _index_,
* or returns a subarray starting at _start_ and
* continuing for _length_ elements, or returns a subarray
* specified by _range_.
* Negative indices count backward from the end of the
* array (-1 is the last element). Returns nil if the index
* (or starting index) are out of range.
*
* a = [ "a", "b", "c", "d", "e" ]
* a[2] + a[0] + a[1] #=> "cab"
* a[6] #=> nil
* a[1, 2] #=> [ "b", "c" ]
* a[1..3] #=> [ "b", "c", "d" ]
* a[4..7] #=> [ "e" ]
* a[6..10] #=> nil
* a[-3, 3] #=> [ "c", "d", "e" ]
* # special cases
* a[5] #=> nil
* a[5, 1] #=> []
* a[5..10] #=> []
*
*/
VALUE
rb_ary_aref(int argc, VALUE *argv, VALUE ary)
{
VALUE arg;
long beg, len;
if (argc == 2) {
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
if (beg < 0) {
beg += RARRAY_LEN(ary);
}
return rb_ary_subseq(ary, beg, len);
}
if (argc != 1) {
rb_scan_args(argc, argv, "11", 0, 0);
}
arg = argv[0];
/* special case - speeding up */
if (FIXNUM_P(arg)) {
return rb_ary_entry(ary, FIX2LONG(arg));
}
/* check if idx is Range */
switch (rb_range_beg_len(arg, &beg, &len, RARRAY_LEN(ary), 0)) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
return rb_ary_subseq(ary, beg, len);
}
return rb_ary_entry(ary, NUM2LONG(arg));
}
/*
* call-seq:
* array.at(index) -> obj or nil
*
* Returns the element at _index_. A
* negative index counts from the end of _self_. Returns +nil+
* if the index is out of range. See also <code>Array#[]</code>.
*
* a = [ "a", "b", "c", "d", "e" ]
* a.at(0) #=> "a"
* a.at(-1) #=> "e"
*/
static VALUE
rb_ary_at(VALUE ary, VALUE pos)
{
return rb_ary_entry(ary, NUM2LONG(pos));
}
/*
* call-seq:
* array.first -> obj or nil
* array.first(n) -> an_array
*
* Returns the first element, or the first +n+ elements, of the array.
* If the array is empty, the first form returns <code>nil</code>, and the
* second form returns an empty array.
*
* a = [ "q", "r", "s", "t" ]
* a.first #=> "q"
* a.first(2) #=> ["q", "r"]
*/
static VALUE
rb_ary_first(int argc, VALUE *argv, VALUE ary)
{
if (argc == 0) {
if (RARRAY_LEN(ary) == 0) return Qnil;
return yard_resolve_stub(RARRAY_PTR(ary)[0]);
}
else {
return ary_shared_first(argc, argv, ary, Qfalse);
}
}
/*
* call-seq:
* array.last -> obj or nil
* array.last(n) -> an_array
*
* Returns the last element(s) of <i>self</i>. If the array is empty,
* the first form returns <code>nil</code>.
*
* a = [ "w", "x", "y", "z" ]
* a.last #=> "z"
* a.last(2) #=> ["y", "z"]
*/
VALUE
rb_ary_last(int argc, VALUE *argv, VALUE ary)
{
if (argc == 0) {
if (RARRAY_LEN(ary) == 0) return Qnil;
return yard_resolve_stub(RARRAY_PTR(ary)[RARRAY_LEN(ary)-1]);
}
else {
return ary_shared_first(argc, argv, ary, Qtrue);
}
}
/*
* call-seq:
* array.fetch(index) -> obj
* array.fetch(index, default ) -> obj
* array.fetch(index) {|index| block } -> obj
*
* Tries to return the element at position <i>index</i>. If the index
* lies outside the array, the first form throws an
* <code>IndexError</code> exception, the second form returns
* <i>default</i>, and the third form returns the value of invoking
* the block, passing in the index. Negative values of <i>index</i>
* count from the end of the array.
*
* a = [ 11, 22, 33, 44 ]
* a.fetch(1) #=> 22
* a.fetch(-1) #=> 44
* a.fetch(4, 'cat') #=> "cat"
* a.fetch(4) { |i| i*i } #=> 16
*/
static VALUE
rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
{
VALUE pos, ifnone;
long block_given;
long idx;
rb_scan_args(argc, argv, "11", &pos, &ifnone);
block_given = rb_block_given_p();
if (block_given && argc == 2) {
rb_warn("block supersedes default value argument");
}
idx = NUM2LONG(pos);
if (idx < 0) {
idx += RARRAY_LEN(ary);
}
if (idx < 0 || RARRAY_LEN(ary) <= idx) {
if (block_given) return rb_yield(pos);
if (argc == 1) {
rb_raise(rb_eIndexError, "index %ld out of array", idx);
}
return ifnone;
}
return yard_resolve_stub(RARRAY_PTR(ary)[idx]);
}
/*
* call-seq:
* array.index(obj) -> int or nil
* array.index {|item| block} -> int or nil
*
* Returns the index of the first object in <i>self</i> such that is
* <code>==</code> to <i>obj</i>. If a block is given instead of an
* argument, returns first object for which <em>block</em> is true.
* Returns <code>nil</code> if no match is found.
*
* a = [ "a", "b", "c" ]
* a.index("b") #=> 1
* a.index("z") #=> nil
* a.index{|x|x=="b"} #=> 1
*/
static VALUE
rb_ary_index(int argc, VALUE *argv, VALUE ary)
{
VALUE val;
long i;
if (rb_scan_args(argc, argv, "01", &val) == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
for (i=0; i<RARRAY_LEN(ary); i++) {
if (RTEST(rb_yield(yard_resolve_stub(RARRAY_PTR(ary)[i])))) {
return LONG2NUM(i);
}
}
}
else {
for (i=0; i<RARRAY_LEN(ary); i++) {
if (rb_equal(yard_resolve_stub(RARRAY_PTR(ary)[i]), val))
return LONG2NUM(i);
}
}
return Qnil;
}
/*
* call-seq:
* array.rindex(obj) -> int or nil
*
* Returns the index of the last object in <i>array</i>
* <code>==</code> to <i>obj</i>. If a block is given instead of an
* argument, returns first object for which <em>block</em> is
* true. Returns <code>nil</code> if no match is found.
*
* a = [ "a", "b", "b", "b", "c" ]
* a.rindex("b") #=> 3
* a.rindex("z") #=> nil
* a.rindex{|x|x=="b"} #=> 3
*/
static VALUE
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
{
VALUE val;
long i = RARRAY_LEN(ary);
if (rb_scan_args(argc, argv, "01", &val) == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
while (i--) {
if (RTEST(rb_yield(yard_resolve_stub(RARRAY_PTR(ary)[i]))))
return LONG2NUM(i);
if (i > RARRAY_LEN(ary)) {
i = RARRAY_LEN(ary);
}
}
}
else {
while (i--) {
if (rb_equal(yard_resolve_stub(RARRAY_PTR(ary)[i]), val))
return LONG2NUM(i);
if (i > RARRAY_LEN(ary)) {
i = RARRAY_LEN(ary);
}
}
}
return Qnil;
}
VALUE
rb_ary_to_ary(VALUE obj)
{
if (TYPE(obj) == T_ARRAY) {
return obj;
}
if (rb_respond_to(obj, rb_intern("to_ary"))) {
return to_ary(obj);
}
return rb_ary_new3(1, obj);
}
static void
rb_ary_splice(VALUE ary, long beg, long len, VALUE rpl)
{
long rlen;
if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
if (beg < 0) {
beg += RARRAY_LEN(ary);
if (beg < 0) {
beg -= RARRAY_LEN(ary);
rb_raise(rb_eIndexError, "index %ld out of array", beg);
}
}
if (RARRAY_LEN(ary) < len || RARRAY_LEN(ary) < beg + len) {
len = RARRAY_LEN(ary) - beg;
}
if (rpl == Qundef) {
rlen = 0;
}
else {