/
RubyArray.java
2459 lines (2023 loc) · 70.8 KB
/
RubyArray.java
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
/*
**** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2005 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2006 Ola Bini <Ola.Bini@ki.se>
* Copyright (C) 2006 Daniel Steer <damian.steer@hp.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import java.lang.reflect.Array;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallbackFactory;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;
import org.jruby.util.Pack;
/**
* The implementation of the built-in class Array in Ruby.
*
* Concurrency: no synchronization is required among readers, but
* all users must synchronize externally with writers.
*
*/
public class RubyArray extends RubyObject implements List {
public static RubyClass createArrayClass(Ruby runtime) {
RubyClass arrayc = runtime.defineClass("Array", runtime.getObject(), ARRAY_ALLOCATOR);
runtime.setArray(arrayc);
arrayc.index = ClassIndex.ARRAY;
arrayc.kindOf = new RubyModule.KindOf() {
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyArray;
}
};
CallbackFactory callbackFactory = runtime.callbackFactory(RubyArray.class);
arrayc.includeModule(runtime.getEnumerable());
arrayc.defineAnnotatedMethods(RubyArray.class);
arrayc.dispatcher = callbackFactory.createDispatcher(arrayc);
return arrayc;
}
private static ObjectAllocator ARRAY_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyArray(runtime, klass);
}
};
public int getNativeTypeIndex() {
return ClassIndex.ARRAY;
}
/** rb_ary_s_create
*
*/
@JRubyMethod(name = "[]", rest = true, frame = true, meta = true)
public static IRubyObject create(IRubyObject klass, IRubyObject[] args, Block block) {
RubyArray arr = (RubyArray) ((RubyClass) klass).allocate();
arr.callInit(IRubyObject.NULL_ARRAY, block);
if (args.length > 0) {
arr.alloc(args.length);
System.arraycopy(args, 0, arr.values, 0, args.length);
arr.realLength = args.length;
}
return arr;
}
/** rb_ary_new2
*
*/
public static final RubyArray newArray(final Ruby runtime, final long len) {
return new RubyArray(runtime, len);
}
public static final RubyArray newArrayLight(final Ruby runtime, final long len) {
return new RubyArray(runtime, len, false);
}
/** rb_ary_new
*
*/
public static final RubyArray newArray(final Ruby runtime) {
return new RubyArray(runtime, ARRAY_DEFAULT_SIZE);
}
/** rb_ary_new
*
*/
public static final RubyArray newArrayLight(final Ruby runtime) {
/* Ruby arrays default to holding 16 elements, so we create an
* ArrayList of the same size if we're not told otherwise
*/
RubyArray arr = new RubyArray(runtime, false);
arr.alloc(ARRAY_DEFAULT_SIZE);
return arr;
}
public static RubyArray newArray(Ruby runtime, IRubyObject obj) {
return new RubyArray(runtime, new IRubyObject[] { obj });
}
/** rb_assoc_new
*
*/
public static RubyArray newArray(Ruby runtime, IRubyObject car, IRubyObject cdr) {
return new RubyArray(runtime, new IRubyObject[] { car, cdr });
}
public static RubyArray newEmptyArray(Ruby runtime) {
return new RubyArray(runtime, NULL_ARRAY);
}
/** rb_ary_new4, rb_ary_new3
*
*/
public static RubyArray newArray(Ruby runtime, IRubyObject[] args) {
RubyArray arr = new RubyArray(runtime, args.length);
System.arraycopy(args, 0, arr.values, 0, args.length);
arr.realLength = args.length;
return arr;
}
public static RubyArray newArrayNoCopy(Ruby runtime, IRubyObject[] args) {
return new RubyArray(runtime, args);
}
public static RubyArray newArrayNoCopy(Ruby runtime, IRubyObject[] args, int begin) {
return new RubyArray(runtime, args, begin);
}
public static RubyArray newArrayNoCopyLight(Ruby runtime, IRubyObject[] args) {
RubyArray arr = new RubyArray(runtime, false);
arr.values = args;
arr.realLength = args.length;
return arr;
}
public static RubyArray newArray(Ruby runtime, Collection collection) {
RubyArray arr = new RubyArray(runtime, collection.size());
collection.toArray(arr.values);
arr.realLength = arr.values.length;
return arr;
}
public static final int ARRAY_DEFAULT_SIZE = 16;
private IRubyObject[] values;
private static final int TMPLOCK_ARR_F = 1 << 9;
private static final int TMPLOCK_OR_FROZEN_ARR_F = TMPLOCK_ARR_F | FROZEN_F;
private volatile boolean isShared = false;
private int begin = 0;
private int realLength = 0;
/*
* plain internal array assignment
*/
private RubyArray(Ruby runtime, IRubyObject[] vals) {
super(runtime, runtime.getArray());
values = vals;
realLength = vals.length;
}
/*
* plain internal array assignment
*/
private RubyArray(Ruby runtime, IRubyObject[] vals, int begin) {
super(runtime, runtime.getArray());
this.values = vals;
this.begin = begin;
this.realLength = vals.length - begin;
this.isShared = true;
}
/* rb_ary_new2
* just allocates the internal array
*/
private RubyArray(Ruby runtime, long length) {
super(runtime, runtime.getArray());
checkLength(length);
alloc((int) length);
}
private RubyArray(Ruby runtime, long length, boolean objectspace) {
super(runtime, runtime.getArray(), objectspace);
checkLength(length);
alloc((int)length);
}
/* rb_ary_new3, rb_ary_new4
* allocates the internal array of size length and copies the 'length' elements
*/
public RubyArray(Ruby runtime, long length, IRubyObject[] vals) {
super(runtime, runtime.getArray());
checkLength(length);
int ilength = (int) length;
alloc(ilength);
if (ilength > 0 && vals.length > 0) System.arraycopy(vals, 0, values, 0, ilength);
realLength = ilength;
}
/* NEWOBJ and OBJSETUP equivalent
* fastest one, for shared arrays, optional objectspace
*/
private RubyArray(Ruby runtime, boolean objectSpace) {
super(runtime, runtime.getArray(), objectSpace);
}
private RubyArray(Ruby runtime) {
super(runtime, runtime.getArray());
alloc(ARRAY_DEFAULT_SIZE);
}
public RubyArray(Ruby runtime, RubyClass klass) {
super(runtime, klass);
alloc(ARRAY_DEFAULT_SIZE);
}
/* Array constructors taking the MetaClass to fulfil MRI Array subclass behaviour
*
*/
private RubyArray(Ruby runtime, RubyClass klass, int length) {
super(runtime, klass);
alloc(length);
}
private RubyArray(Ruby runtime, RubyClass klass, long length) {
super(runtime, klass);
checkLength(length);
alloc((int)length);
}
private RubyArray(Ruby runtime, RubyClass klass, long length, boolean objectspace) {
super(runtime, klass, objectspace);
checkLength(length);
alloc((int)length);
}
private RubyArray(Ruby runtime, RubyClass klass, boolean objectSpace) {
super(runtime, klass, objectSpace);
}
private RubyArray(Ruby runtime, RubyClass klass, RubyArray original) {
super(runtime, klass);
realLength = original.realLength;
alloc(realLength);
System.arraycopy(original.values, original.begin, values, 0, realLength);
}
private final IRubyObject[] reserve(int length) {
return new IRubyObject[length];
}
private final void alloc(int length) {
values = new IRubyObject[length];
}
private final void realloc(int newLength) {
IRubyObject[] reallocated = new IRubyObject[newLength];
System.arraycopy(values, 0, reallocated, 0, newLength > realLength ? realLength : newLength);
values = reallocated;
}
private final void checkLength(long length) {
if (length < 0) {
throw getRuntime().newArgumentError("negative array size (or size too big)");
}
if (length >= Integer.MAX_VALUE) {
throw getRuntime().newArgumentError("array size too big");
}
}
/** Getter for property list.
* @return Value of property list.
*/
public List getList() {
return Arrays.asList(toJavaArray());
}
public int getLength() {
return realLength;
}
public IRubyObject[] toJavaArray() {
IRubyObject[] copy = reserve(realLength);
System.arraycopy(values, begin, copy, 0, realLength);
return copy;
}
public IRubyObject[] toJavaArrayUnsafe() {
return !isShared ? values : toJavaArray();
}
public IRubyObject[] toJavaArrayMaybeUnsafe() {
return (!isShared && begin == 0 && values.length == realLength) ? values : toJavaArray();
}
/** rb_ary_make_shared
*
*/
private final RubyArray makeShared(int beg, int len, RubyClass klass) {
return makeShared(beg, len, klass, klass.getRuntime().isObjectSpaceEnabled());
}
/** rb_ary_make_shared
*
*/
private final RubyArray makeShared(int beg, int len, RubyClass klass, boolean objectSpace) {
RubyArray sharedArray = new RubyArray(getRuntime(), klass, objectSpace);
isShared = true;
sharedArray.values = values;
sharedArray.isShared = true;
sharedArray.begin = beg;
sharedArray.realLength = len;
return sharedArray;
}
/** rb_ary_modify_check
*
*/
private final void modifyCheck() {
if ((flags & TMPLOCK_OR_FROZEN_ARR_F) != 0) {
if ((flags & FROZEN_F) != 0) throw getRuntime().newFrozenError("array");
if ((flags & TMPLOCK_ARR_F) != 0) throw getRuntime().newTypeError("can't modify array during iteration");
}
if (!isTaint() && getRuntime().getSafeLevel() >= 4) {
throw getRuntime().newSecurityError("Insecure: can't modify array");
}
}
/** rb_ary_modify
*
*/
private final void modify() {
modifyCheck();
if (isShared) {
IRubyObject[] vals = reserve(realLength);
isShared = false;
System.arraycopy(values, begin, vals, 0, realLength);
begin = 0;
values = vals;
}
}
/* ================
* Instance Methods
* ================
*/
/** rb_ary_initialize
*
*/
@JRubyMethod(name = "initialize", required = 0, optional = 2, frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(IRubyObject[] args, Block block) {
int argc = args.length;
Ruby runtime = getRuntime();
if (argc == 0) {
modifyCheck();
realLength = 0;
if (block.isGiven()) runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
return this;
}
if (argc == 1 && !(args[0] instanceof RubyFixnum)) {
IRubyObject val = args[0].checkArrayType();
if (!val.isNil()) {
replace(val);
return this;
}
}
long len = RubyNumeric.num2long(args[0]);
if (len < 0) throw runtime.newArgumentError("negative array size");
if (len >= Integer.MAX_VALUE) throw runtime.newArgumentError("array size too big");
int ilen = (int) len;
modify();
if (ilen > values.length) values = reserve(ilen);
if (block.isGiven()) {
if (argc == 2) {
runtime.getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
}
ThreadContext context = runtime.getCurrentContext();
for (int i = 0; i < ilen; i++) {
store(i, block.yield(context, new RubyFixnum(runtime, i)));
realLength = i + 1;
}
} else {
Arrays.fill(values, 0, ilen, (argc == 2) ? args[1] : runtime.getNil());
realLength = ilen;
}
return this;
}
/** rb_ary_initialize_copy
*
*/
@JRubyMethod(name = {"initialize_copy"}, required = 1, visibility=Visibility.PRIVATE)
public IRubyObject initialize_copy(IRubyObject orig) {
return this.replace(orig);
}
/** rb_ary_replace
*
*/
@JRubyMethod(name = {"replace"}, required = 1)
public IRubyObject replace(IRubyObject orig) {
modifyCheck();
RubyArray origArr = orig.convertToArray();
if (this == orig) return this;
origArr.isShared = true;
isShared = true;
values = origArr.values;
realLength = origArr.realLength;
begin = origArr.begin;
return this;
}
/** rb_ary_to_s
*
*/
@JRubyMethod(name = "to_s")
public IRubyObject to_s() {
if (realLength == 0) return getRuntime().newString("");
return join(getRuntime().getGlobalVariables().get("$,"));
}
public boolean includes(IRubyObject item) {
final ThreadContext context = getRuntime().getCurrentContext();
int begin = this.begin;
for (int i = begin; i < begin + realLength; i++) {
if (equalInternal(context, values[i], item).isTrue()) return true;
}
return false;
}
/** rb_ary_hash
*
*/
@JRubyMethod(name = "hash")
public RubyFixnum hash() {
int h = realLength;
Ruby runtime = getRuntime();
ThreadContext context = runtime.getCurrentContext();
int begin = this.begin;
for (int i = begin; i < begin + realLength; i++) {
h = (h << 1) | (h < 0 ? 1 : 0);
h ^= RubyNumeric.num2long(values[i].callMethod(context, MethodIndex.HASH, "hash"));
}
return runtime.newFixnum(h);
}
/** rb_ary_store
*
*/
public final IRubyObject store(long index, IRubyObject value) {
if (index < 0) {
index += realLength;
if (index < 0) {
throw getRuntime().newIndexError("index " + (index - realLength) + " out of array");
}
}
modify();
if (index >= realLength) {
if (index >= values.length) {
long newLength = values.length >> 1;
if (newLength < ARRAY_DEFAULT_SIZE) newLength = ARRAY_DEFAULT_SIZE;
newLength += index;
if (newLength >= Integer.MAX_VALUE) {
throw getRuntime().newArgumentError("index too big");
}
realloc((int) newLength);
}
if(index != realLength) Arrays.fill(values, realLength, (int) index + 1, getRuntime().getNil());
realLength = (int) index + 1;
}
values[(int) index] = value;
return value;
}
/** rb_ary_elt - faster
*
*/
private final IRubyObject elt(long offset) {
if (realLength == 0 || offset < 0 || offset >= realLength) return getRuntime().getNil();
return values[begin + (int) offset];
}
/** rb_ary_elt - faster
*
*/
private final IRubyObject elt(int offset) {
if (realLength == 0 || offset < 0 || offset >= realLength) return getRuntime().getNil();
return values[begin + offset];
}
/** rb_ary_elt - faster
*
*/
private final IRubyObject elt_f(long offset) {
if (realLength == 0 || offset >= realLength) return getRuntime().getNil();
return values[begin + (int) offset];
}
/** rb_ary_elt - faster
*
*/
private final IRubyObject elt_f(int offset) {
if (realLength == 0 || offset >= realLength) return getRuntime().getNil();
return values[begin + offset];
}
/** rb_ary_entry
*
*/
public final IRubyObject entry(long offset) {
return (offset < 0 ) ? elt(offset + realLength) : elt_f(offset);
}
/** rb_ary_entry
*
*/
public final IRubyObject entry(int offset) {
return (offset < 0 ) ? elt(offset + realLength) : elt_f(offset);
}
public final IRubyObject eltInternal(int offset) {
return values[begin + offset];
}
public final IRubyObject eltInternalSet(int offset, IRubyObject item) {
return values[begin + offset] = item;
}
/** rb_ary_fetch
*
*/
@JRubyMethod(name = "fetch", required = 1, optional = 1, frame = true)
public IRubyObject fetch(IRubyObject[] args, Block block) {
if (args.length == 2 && block.isGiven()) {
getRuntime().getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
}
long index = RubyNumeric.num2long(args[0]);
if (index < 0) index += realLength;
if (index < 0 || index >= realLength) {
if (block.isGiven()) return block.yield(getRuntime().getCurrentContext(), args[0]);
if (args.length == 1) {
throw getRuntime().newIndexError("index " + index + " out of array");
}
return args[1];
}
return values[begin + (int) index];
}
/** rb_ary_to_ary
*
*/
private static RubyArray aryToAry(IRubyObject obj) {
if (obj instanceof RubyArray) return (RubyArray) obj;
if (obj.respondsTo("to_ary")) return obj.convertToArray();
RubyArray arr = new RubyArray(obj.getRuntime(), false); // possibly should not in object space
arr.alloc(1);
arr.values[0] = obj;
arr.realLength = 1;
return arr;
}
/** rb_ary_splice
*
*/
private final void splice(long beg, long len, IRubyObject rpl) {
int rlen;
if (len < 0) throw getRuntime().newIndexError("negative length (" + len + ")");
if (beg < 0) {
beg += realLength;
if (beg < 0) {
beg -= realLength;
throw getRuntime().newIndexError("index " + beg + " out of array");
}
}
if (beg + len > realLength) len = realLength - beg;
RubyArray rplArr;
if (rpl == null || rpl.isNil()) {
rplArr = null;
rlen = 0;
} else {
rplArr = aryToAry(rpl);
rlen = rplArr.realLength;
}
modify();
if (beg >= realLength) {
len = beg + rlen;
if (len >= values.length) {
int tryNewLength = values.length + (values.length >> 1);
realloc(len > tryNewLength ? (int)len : tryNewLength);
}
Arrays.fill(values, realLength, (int) beg, getRuntime().getNil());
if (rlen > 0) {
System.arraycopy(rplArr.values, rplArr.begin, values, (int) beg, rlen);
}
realLength = (int) len;
} else {
long alen;
if (beg + len > realLength) len = realLength - beg;
alen = realLength + rlen - len;
if (alen >= values.length) {
int tryNewLength = values.length + (values.length >> 1);
realloc(alen > tryNewLength ? (int)alen : tryNewLength);
}
if (len != rlen) {
System.arraycopy(values, (int) (beg + len), values, (int) beg + rlen, realLength - (int) (beg + len));
realLength = (int) alen;
}
if (rlen > 0) {
System.arraycopy(rplArr.values, rplArr.begin, values, (int) beg, rlen);
}
}
}
/** rb_ary_insert
*
*/
@JRubyMethod(name = "insert", required = 1, rest = true)
public IRubyObject insert(IRubyObject[] args) {
if (args.length == 1) return this;
if (args.length < 1) {
throw getRuntime().newArgumentError("wrong number of arguments (at least 1)");
}
long pos = RubyNumeric.num2long(args[0]);
if (pos == -1) pos = realLength;
if (pos < 0) pos++;
RubyArray inserted = new RubyArray(getRuntime(), false);
inserted.values = args;
inserted.begin = 1;
inserted.realLength = args.length - 1;
splice(pos, 0, inserted); // rb_ary_new4
return this;
}
/** rb_ary_dup
*
*/
public final RubyArray aryDup() {
RubyArray dup = new RubyArray(getRuntime(), getMetaClass(), this);
dup.flags |= flags & TAINTED_F; // from DUP_SETUP
// rb_copy_generic_ivar from DUP_SETUP here ...unlikely..
return dup;
}
/** rb_ary_transpose
*
*/
@JRubyMethod(name = "transpose")
public RubyArray transpose() {
RubyArray tmp, result = null;
int alen = realLength;
if (alen == 0) return aryDup();
Ruby runtime = getRuntime();
int elen = -1;
int end = begin + alen;
for (int i = begin; i < end; i++) {
tmp = elt(i).convertToArray();
if (elen < 0) {
elen = tmp.realLength;
result = new RubyArray(runtime, elen);
for (int j = 0; j < elen; j++) {
result.store(j, new RubyArray(runtime, alen));
}
} else if (elen != tmp.realLength) {
throw runtime.newIndexError("element size differs (" + tmp.realLength
+ " should be " + elen + ")");
}
for (int j = 0; j < elen; j++) {
((RubyArray) result.elt(j)).store(i - begin, tmp.elt(j));
}
}
return result;
}
/** rb_values_at (internal)
*
*/
private final IRubyObject values_at(long olen, IRubyObject[] args) {
RubyArray result = new RubyArray(getRuntime(), args.length);
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof RubyFixnum) {
result.append(entry(((RubyFixnum)args[i]).getLongValue()));
continue;
}
long beglen[];
if (!(args[i] instanceof RubyRange)) {
} else if ((beglen = ((RubyRange) args[i]).begLen(olen, 0)) == null) {
continue;
} else {
int beg = (int) beglen[0];
int len = (int) beglen[1];
int end = begin + len;
for (int j = begin; j < end; j++) {
result.append(entry(j + beg));
}
continue;
}
result.append(entry(RubyNumeric.num2long(args[i])));
}
return result;
}
/** rb_values_at
*
*/
@JRubyMethod(name = "values_at", rest = true)
public IRubyObject values_at(IRubyObject[] args) {
return values_at(realLength, args);
}
/** rb_ary_subseq
*
*/
public IRubyObject subseq(long beg, long len) {
if (beg > realLength || beg < 0 || len < 0) return getRuntime().getNil();
if (beg + len > realLength) {
len = realLength - beg;
if (len < 0) len = 0;
}
if (len == 0) return new RubyArray(getRuntime(), getMetaClass(), 0);
return makeShared(begin + (int) beg, (int) len, getMetaClass());
}
/** rb_ary_subseq
*
*/
public IRubyObject subseqLight(long beg, long len) {
if (beg > realLength || beg < 0 || len < 0) return getRuntime().getNil();
if (beg + len > realLength) {
len = realLength - beg;
if (len < 0) len = 0;
}
if (len == 0) return new RubyArray(getRuntime(), getMetaClass(), 0, false);
return makeShared(begin + (int) beg, (int) len, getMetaClass(), false);
}
/** rb_ary_length
*
*/
@JRubyMethod(name = "length", alias = "size")
public RubyFixnum length() {
return getRuntime().newFixnum(realLength);
}
/** rb_ary_push - specialized rb_ary_store
*
*/
@JRubyMethod(name = "<<", required = 1)
public RubyArray append(IRubyObject item) {
modify();
if (realLength == values.length) {
if (realLength == Integer.MAX_VALUE) throw getRuntime().newArgumentError("index too big");
long newLength = values.length + (values.length >> 1);
if ( newLength > Integer.MAX_VALUE ) {
newLength = Integer.MAX_VALUE;
}else if ( newLength < ARRAY_DEFAULT_SIZE ) {
newLength = ARRAY_DEFAULT_SIZE;
}
realloc((int) newLength);
}
values[realLength++] = item;
return this;
}
/** rb_ary_push_m
* FIXME: Whis is this named "push_m"?
*/
@JRubyMethod(name = "push", rest = true)
public RubyArray push_m(IRubyObject[] items) {
for (int i = 0; i < items.length; i++) {
append(items[i]);
}
return this;
}
/** rb_ary_pop
*
*/
@JRubyMethod(name = "pop")
public IRubyObject pop() {
modifyCheck();
if (realLength == 0) return getRuntime().getNil();
if (!isShared) {
int index = begin + --realLength;
IRubyObject obj = values[index];
values[index] = null;
return obj;
}
return values[begin + --realLength];
}
/** rb_ary_shift
*
*/
@JRubyMethod(name = "shift")
public IRubyObject shift() {
modify();
if (realLength == 0) return getRuntime().getNil();
IRubyObject obj = values[begin];
values[begin] = null;
isShared = true;
begin++;
realLength--;
return obj;
}
/** rb_ary_unshift
*
*/
public RubyArray unshift(IRubyObject item) {
modify();
if (realLength == values.length) {
int newLength = values.length >> 1;
if (newLength < ARRAY_DEFAULT_SIZE) newLength = ARRAY_DEFAULT_SIZE;
newLength += values.length;
realloc(newLength);
}
System.arraycopy(values, 0, values, 1, realLength);
realLength++;
values[0] = item;
return this;
}
/** rb_ary_unshift_m
*
*/
@JRubyMethod(name = "unshift", rest = true)
public RubyArray unshift_m(IRubyObject[] items) {
long len = realLength;
if (items.length == 0) return this;
store(len + items.length - 1, getRuntime().getNil());
// it's safe to use zeroes here since modified by store()
System.arraycopy(values, 0, values, items.length, (int) len);
System.arraycopy(items, 0, values, 0, items.length);
return this;
}
/** rb_ary_includes
*
*/
@JRubyMethod(name = "include?", required = 1)
public RubyBoolean include_p(IRubyObject item) {
return getRuntime().newBoolean(includes(item));
}
/** rb_ary_frozen_p
*
*/
@JRubyMethod(name = "frozen?")
public RubyBoolean frozen_p() {
return getRuntime().newBoolean(isFrozen() || (flags & TMPLOCK_ARR_F) != 0);