forked from jruby/jruby
/
RubyArray.java
1613 lines (1402 loc) · 44.9 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>
*
* 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.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaUtil;
import org.jruby.javasupport.util.ConversionIterator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.builtin.meta.ArrayMetaClass;
import org.jruby.runtime.builtin.meta.StringMetaClass;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.Pack;
import org.jruby.util.collections.IdentitySet;
/**
* The implementation of the built-in class Array in Ruby.
*/
public class RubyArray extends RubyObject implements List {
private List list;
private boolean tmpLock;
private RubyArray(IRuby runtime, List list) {
super(runtime, runtime.getClass("Array"));
this.list = list;
}
/** Getter for property list.
* @return Value of property list.
*/
public List getList() {
return list;
}
public IRubyObject[] toJavaArray() {
return (IRubyObject[])list.toArray(new IRubyObject[getLength()]);
}
public RubyArray convertToArray() {
return this;
}
/** Getter for property tmpLock.
* @return Value of property tmpLock.
*/
public boolean isTmpLock() {
return tmpLock;
}
/** Setter for property tmpLock.
* @param tmpLock New value of property tmpLock.
*/
public void setTmpLock(boolean tmpLock) {
this.tmpLock = tmpLock;
}
public int getLength() {
return list.size();
}
public boolean includes(IRubyObject item) {
for (int i = 0, n = getLength(); i < n; i++) {
if (item.callMethod(getRuntime().getCurrentContext(), "==", entry(i)).isTrue()) {
return true;
}
}
return false;
}
public RubyFixnum hash() {
return getRuntime().newFixnum(list.hashCode());
}
/** rb_ary_modify
*
*/
public void modify() {
testFrozen("Array");
if (isTmpLock()) {
throw getRuntime().newTypeError("can't modify array during sort");
}
if (isTaint() && getRuntime().getSafeLevel() >= 4) {
throw getRuntime().newSecurityError("Insecure: can't modify array");
}
}
/* if list's size is not at least 'toLength', add nil's until it is */
private void autoExpand(long toLength) {
//list.ensureCapacity((int) toLength);
for (int i = getLength(); i < toLength; i++) {
list.add(getRuntime().getNil());
}
}
/** rb_ary_store
*
*/
private IRubyObject store(long index, IRubyObject value) {
modify();
if (index < 0) {
index += getLength();
if (index < 0) {
throw getRuntime().newIndexError("index " + (index - getLength()) + " out of array");
}
}
autoExpand(index + 1);
list.set((int) index, value);
return value;
}
public IRubyObject entry(long offset) {
return entry(offset, false);
}
/** rb_ary_entry
*
*/
public IRubyObject entry(long offset, boolean throwException) {
if (getLength() == 0) {
if (throwException) {
throw getRuntime().newIndexError("index " + offset + " out of array");
}
return getRuntime().getNil();
}
if (offset < 0) {
offset += getLength();
}
if (offset < 0 || getLength() <= offset) {
if (throwException) {
throw getRuntime().newIndexError("index " + offset + " out of array");
}
return getRuntime().getNil();
}
return (IRubyObject) list.get((int) offset);
}
public IRubyObject fetch(IRubyObject[] args) {
checkArgumentCount(args, 1, 2);
RubyInteger index = args[0].convertToInteger();
try {
return entry(index.getLongValue(), true);
} catch (RaiseException e) {
ThreadContext tc = getRuntime().getCurrentContext();
// FIXME: use constant or method for IndexError lookup?
RubyException raisedException = e.getException();
if (raisedException.isKindOf(getRuntime().getClassFromPath("IndexError"))) {
if (args.length > 1) {
return args[1];
} else if (tc.isBlockGiven()) {
return tc.yield(index);
}
}
throw e;
}
}
public IRubyObject insert(IRubyObject[] args) {
checkArgumentCount(args, 1, -1);
// ruby does not bother to bounds check index, if no elements are to be added.
if (args.length == 1) {
return this;
}
// too negative of an offset will throw an IndexError
long offset = args[0].convertToInteger().getLongValue();
if (offset < 0 && getLength() + offset < 0) {
throw getRuntime().newIndexError("index " +
(getLength() + offset) + " out of array");
}
// An offset larger than the current length will pad with nils
// to length
if (offset > getLength()) {
long difference = offset - getLength();
IRubyObject nil = getRuntime().getNil();
for (long i = 0; i < difference; i++) {
list.add(nil);
}
}
if (offset < 0) {
offset += getLength() + 1;
}
for (int i = 1; i < args.length; i++) {
list.add((int) (offset + i - 1), args[i]);
}
return this;
}
public RubyArray transpose() {
RubyArray newArray = getRuntime().newArray();
int length = getLength();
if (length == 0) {
return newArray;
}
for (int i = 0; i < length; i++) {
if (!(entry(i) instanceof RubyArray)) {
throw getRuntime().newTypeError("Some error");
}
}
int width = ((RubyArray) entry(0)).getLength();
for (int j = 0; j < width; j++) {
RubyArray columnArray = getRuntime().newArray(length);
for (int i = 0; i < length; i++) {
try {
columnArray.append((IRubyObject) ((RubyArray) entry(i)).list.get(j));
} catch (IndexOutOfBoundsException e) {
throw getRuntime().newIndexError("element size differ (" + i +
" should be " + width + ")");
}
}
newArray.append(columnArray);
}
return newArray;
}
public IRubyObject values_at(IRubyObject[] args) {
RubyArray newArray = getRuntime().newArray();
for (int i = 0; i < args.length; i++) {
IRubyObject o = aref(new IRubyObject[] {args[i]});
if (args[i] instanceof RubyRange) {
if (o instanceof RubyArray) {
for (Iterator j = ((RubyArray) o).getList().iterator(); j.hasNext();) {
newArray.append((IRubyObject) j.next());
}
}
} else {
newArray.append(o);
}
}
return newArray;
}
/** rb_ary_unshift
*
*/
public RubyArray unshift(IRubyObject item) {
modify();
list.add(0, item);
return this;
}
/** rb_ary_subseq
*
*/
public IRubyObject subseq(long beg, long len) {
int length = getLength();
if (beg > length || beg < 0 || len < 0) {
return getRuntime().getNil();
}
if (beg + len > length) {
len = length - beg;
}
return len <= 0 ? getRuntime().newArray(0) :
getRuntime().newArray(
new ArrayList(list.subList((int)beg, (int) (len + beg))));
}
/** rb_ary_replace
* @todo change the algorythm to make it efficient
* there should be no need to do any deletion or addition
* when the replacing object is an array of the same length
* and in any case we should minimize them, they are costly
*/
public void replace(long beg, long len, IRubyObject repl) {
int length = getLength();
if (len < 0) {
throw getRuntime().newIndexError("Negative array length: " + len);
}
if (beg < 0) {
beg += length;
}
if (beg < 0) {
throw getRuntime().newIndexError("Index out of bounds: " + beg);
}
modify();
for (int i = 0; beg < getLength() && i < len; i++) {
list.remove((int) beg);
}
autoExpand(beg);
if (repl instanceof RubyArray) {
List repList = ((RubyArray) repl).getList();
//list.ensureCapacity(getLength() + repList.size());
list.addAll((int) beg, new ArrayList(repList));
} else if (!repl.isNil()) {
list.add((int) beg, repl);
}
}
private boolean flatten(List array) {
return flatten(array, new IdentitySet());
}
private boolean flatten(List array, IdentitySet visited) {
if (visited.contains(array)) {
throw getRuntime().newArgumentError("tried to flatten recursive array");
}
visited.add(array);
boolean isModified = false;
for (int i = array.size() - 1; i >= 0; i--) {
if (array.get(i) instanceof RubyArray) {
List ary2 = ((RubyArray) array.remove(i)).getList();
flatten(ary2, visited);
array.addAll(i, ary2);
isModified = true;
}
}
visited.remove(array);
return isModified;
}
//
// Methods of the Array Class (rb_ary_*):
//
/** rb_ary_new2
*
*/
public static final RubyArray newArray(final IRuby runtime, final long len) {
return new RubyArray(runtime, new ArrayList((int) len));
}
/** rb_ary_new
*
*/
public static final RubyArray newArray(final IRuby runtime) {
/* Ruby arrays default to holding 16 elements, so we create an
* ArrayList of the same size if we're not told otherwise
*/
return new RubyArray(runtime, new ArrayList(16));
}
/**
*
*/
public static RubyArray newArray(IRuby runtime, IRubyObject obj) {
ArrayList list = new ArrayList(1);
list.add(obj);
return new RubyArray(runtime, list);
}
/** rb_assoc_new
*
*/
public static RubyArray newArray(IRuby runtime, IRubyObject car, IRubyObject cdr) {
ArrayList list = new ArrayList(2);
list.add(car);
list.add(cdr);
return new RubyArray(runtime, list);
}
public static final RubyArray newArray(final IRuby runtime, final List list) {
return new RubyArray(runtime, list);
}
public static RubyArray newArray(IRuby runtime, IRubyObject[] args) {
final ArrayList list = new ArrayList(args.length);
for (int i = 0; i < args.length; i++) {
list.add(args[i]);
}
return new RubyArray(runtime, list);
}
/** rb_ary_length
*
*/
public RubyFixnum length() {
return getRuntime().newFixnum(getLength());
}
/** rb_ary_push_m
*
*/
public RubyArray push(IRubyObject[] items) {
modify();
boolean tainted = false;
for (int i = 0; i < items.length; i++) {
tainted |= items[i].isTaint();
list.add(items[i]);
}
setTaint(isTaint() || tainted);
return this;
}
public RubyArray append(IRubyObject value) {
modify();
list.add(value);
infectBy(value);
return this;
}
/** rb_ary_pop
*
*/
public IRubyObject pop() {
modify();
int length = getLength();
return length == 0 ? getRuntime().getNil() :
(IRubyObject) list.remove(length - 1);
}
/** rb_ary_shift
*
*/
public IRubyObject shift() {
modify();
return getLength() == 0 ? getRuntime().getNil() :
(IRubyObject) list.remove(0);
}
/** rb_ary_unshift_m
*
*/
public RubyArray unshift(IRubyObject[] items) {
modify();
boolean taint = false;
for (int i = 0; i < items.length; i++) {
taint |= items[i].isTaint();
list.add(i, items[i]);
}
setTaint(isTaint() || taint);
return this;
}
public RubyBoolean include_p(IRubyObject item) {
return getRuntime().newBoolean(includes(item));
}
/** rb_ary_frozen_p
*
*/
public RubyBoolean frozen() {
return getRuntime().newBoolean(isFrozen() || isTmpLock());
}
/** rb_ary_initialize
*/
public IRubyObject initialize(IRubyObject[] args) {
int argc = checkArgumentCount(args, 0, 2);
RubyArray arrayInitializer = null;
long len = 0;
if (argc > 0) {
if (args[0] instanceof RubyArray) {
arrayInitializer = (RubyArray)args[0];
} else {
len = convertToLong(args[0]);
}
}
modify();
// Array initializer is provided
if (arrayInitializer != null) {
list = new ArrayList(arrayInitializer.list);
return this;
}
// otherwise, continue with Array.new(fixnum, obj)
if (len < 0) {
throw getRuntime().newArgumentError("negative array size");
}
if (len > Integer.MAX_VALUE) {
throw getRuntime().newArgumentError("array size too big");
}
list = new ArrayList((int) len);
ThreadContext tc = getRuntime().getCurrentContext();
if (len > 0) {
if (tc.isBlockGiven()) {
// handle block-based array initialization
for (int i = 0; i < len; i++) {
list.add(tc.yield(new RubyFixnum(getRuntime(), i)));
}
} else {
IRubyObject obj = (argc == 2) ? args[1] : getRuntime().getNil();
list.addAll(Collections.nCopies((int)len, obj));
}
}
return this;
}
/** rb_ary_aref
*/
public IRubyObject aref(IRubyObject[] args) {
int argc = checkArgumentCount(args, 1, 2);
if (argc == 2) {
long beg = RubyNumeric.fix2long(args[0]);
long len = RubyNumeric.fix2long(args[1]);
if (beg < 0) {
beg += getLength();
}
return subseq(beg, len);
}
if (args[0] instanceof RubyFixnum) {
return entry(RubyNumeric.fix2long(args[0]));
}
if (args[0] instanceof RubyBignum) {
throw getRuntime().newIndexError("index too big");
}
if (args[0] instanceof RubyRange) {
long[] begLen = ((RubyRange) args[0]).getBeginLength(getLength(), true, false);
return begLen == null ? newArray(getRuntime()) : subseq(begLen[0], begLen[1]);
}
return entry(args[0].convertToInteger().getLongValue());
}
/** rb_ary_aset
*
*/
public IRubyObject aset(IRubyObject[] args) {
int argc = checkArgumentCount(args, 2, 3);
if (argc == 3) {
long beg = args[0].convertToInteger().getLongValue();
long len = args[1].convertToInteger().getLongValue();
replace(beg, len, args[2]);
return args[2];
}
if (args[0] instanceof RubyRange) {
long[] begLen = ((RubyRange) args[0]).getBeginLength(getLength(), false, true);
replace(begLen[0], begLen[1], args[1]);
return args[1];
}
if (args[0] instanceof RubyBignum) {
throw getRuntime().newIndexError("Index too large");
}
return store(args[0].convertToInteger().getLongValue(), args[1]);
}
/** rb_ary_at
*
*/
public IRubyObject at(IRubyObject pos) {
return entry(convertToLong(pos));
}
private long convertToLong(IRubyObject pos) {
if (pos instanceof RubyNumeric) {
return ((RubyNumeric) pos).getLongValue();
}
throw getRuntime().newTypeError("cannot convert " + pos.getType().getBaseName() + " to Integer");
}
/** rb_ary_concat
*
*/
public RubyArray concat(IRubyObject obj) {
modify();
RubyArray other = obj.convertToArray();
list.addAll(other.getList());
infectBy(other);
return this;
}
/** rb_ary_inspect
*
*/
public IRubyObject inspect() {
int length = getLength();
if (length == 0) {
return getRuntime().newString("[]");
}
RubyString result = getRuntime().newString("[");
RubyString separator = getRuntime().newString(", ");
for (int i = 0; i < length; i++) {
if (i > 0) {
result.append(separator);
}
result.append(entry(i).callMethod(getRuntime().getCurrentContext(), "inspect"));
}
result.cat("]");
return result;
}
/** rb_ary_first
*
*/
public IRubyObject first(IRubyObject[] args) {
checkArgumentCount(args, 0, 1);
if (args.length == 0) {
return getLength() == 0 ? getRuntime().getNil() : entry(0);
}
// TODO: See if enough integer-only conversions to make this
// convenience function (which could replace RubyNumeric#fix2long).
if (!(args[0] instanceof RubyInteger)) {
throw getRuntime().newTypeError("Cannot convert " +
args[0].getType() + " into Integer");
}
long length = ((RubyInteger)args[0]).getLongValue();
if (length < 0) {
throw getRuntime().newArgumentError(
"negative array size (or size too big)");
}
return subseq(0, length);
}
/** rb_ary_last
*
*/
public IRubyObject last(IRubyObject[] args) {
int count = checkArgumentCount(args, 0, 1);
int length = getLength();
int listSize = list.size();
int sublistSize = 0;
int startIndex = 0;
switch (count) {
case 0:
return length == 0 ? getRuntime().getNil() : entry(length - 1);
case 1:
sublistSize = RubyNumeric.fix2int(args[0]);
if (sublistSize == 0) {
return getRuntime().newArray();
}
if (sublistSize < 0) {
throw getRuntime().newArgumentError("negative array size (or size too big)");
}
startIndex = (sublistSize > listSize) ? 0 : listSize - sublistSize;
return getRuntime().newArray(list.subList(startIndex, listSize));
default:
assert false;
return null;
}
}
/** rb_ary_each
*
*/
public IRubyObject each() {
for (int i = 0, len = getLength(); i < len; i++) {
getRuntime().getCurrentContext().yield(entry(i));
}
return this;
}
/** rb_ary_each_index
*
*/
public IRubyObject each_index() {
for (int i = 0, len = getLength(); i < len; i++) {
getRuntime().getCurrentContext().yield(getRuntime().newFixnum(i));
}
return this;
}
/** rb_ary_reverse_each
*
*/
public IRubyObject reverse_each() {
for (long i = getLength(); i > 0; i--) {
getRuntime().getCurrentContext().yield(entry(i - 1));
}
return this;
}
/** rb_ary_join
*
*/
public RubyString join(RubyString sep) {
StringBuffer buf = new StringBuffer();
int length = getLength();
if (length == 0) {
getRuntime().newString("");
}
boolean taint = isTaint() || sep.isTaint();
RubyString str;
IRubyObject tmp = null;
for (long i = 0; i < length; i++) {
tmp = entry(i);
taint |= tmp.isTaint();
if (tmp instanceof RubyString) {
// do nothing
} else if (tmp instanceof RubyArray) {
tmp = ((RubyArray) tmp).join(sep);
} else {
tmp = RubyString.objAsString(tmp);
}
if (i > 0 && !sep.isNil()) {
buf.append(sep.toString());
}
buf.append(((RubyString)tmp).toString());
}
str = RubyString.newString(getRuntime(), buf.toString());
str.setTaint(taint);
return str;
}
/** rb_ary_join_m
*
*/
public RubyString join(IRubyObject[] args) {
int argc = checkArgumentCount(args, 0, 1);
IRubyObject sep = (argc == 1) ? args[0] : getRuntime().getGlobalVariables().get("$,");
return join(sep.isNil() ? getRuntime().newString("") : RubyString.stringValue(sep));
}
/** rb_ary_to_s
*
*/
public IRubyObject to_s() {
IRubyObject separatorObject = getRuntime().getGlobalVariables().get("$,");
RubyString separator;
if (separatorObject.isNil()) {
separator = getRuntime().newString("");
} else {
separator = RubyString.stringValue(separatorObject);
}
return join(separator);
}
/** rb_ary_to_a
*
*/
public RubyArray to_a() {
return this;
}
public IRubyObject to_ary() {
return this;
}
/** rb_ary_equal
*
*/
public IRubyObject array_op_equal(IRubyObject obj) {
if (this == obj) {
return getRuntime().getTrue();
}
RubyArray ary;
if (!(obj instanceof RubyArray)) {
if (obj.respondsTo("to_ary")) {
ary = obj.convertToArray();
} else {
return getRuntime().getFalse();
}
} else {
ary = (RubyArray) obj;
}
int length = getLength();
if (length != ary.getLength()) {
return getRuntime().getFalse();
}
for (long i = 0; i < length; i++) {
if (!entry(i).callMethod(getRuntime().getCurrentContext(), "==", ary.entry(i)).isTrue()) {
return getRuntime().getFalse();
}
}
return getRuntime().getTrue();
}
/** rb_ary_eql
*
*/
public RubyBoolean eql(IRubyObject obj) {
if (!(obj instanceof RubyArray)) {
return getRuntime().getFalse();
}
int length = getLength();
RubyArray ary = (RubyArray) obj;
if (length != ary.getLength()) {
return getRuntime().getFalse();
}
for (long i = 0; i < length; i++) {
if (!entry(i).callMethod(getRuntime().getCurrentContext(), "eql?", ary.entry(i)).isTrue()) {
return getRuntime().getFalse();
}
}
return getRuntime().getTrue();
}
/** rb_ary_compact_bang
*
*/
public IRubyObject compact_bang() {
modify();
boolean isChanged = false;
for (int i = getLength() - 1; i >= 0; i--) {
if (entry(i).isNil()) {
list.remove(i);
isChanged = true;
}
}
return isChanged ? (IRubyObject) this : (IRubyObject) getRuntime().getNil();
}
/** rb_ary_compact
*
*/
public IRubyObject compact() {
RubyArray ary = (RubyArray) dup();
ary.compact_bang();
return ary;
}
/** rb_ary_empty_p
*
*/
public IRubyObject empty_p() {
return getLength() == 0 ? getRuntime().getTrue() : getRuntime().getFalse();
}
/** rb_ary_clear
*
*/
public IRubyObject rb_clear() {
modify();
list.clear();
return this;
}
/** rb_ary_fill
*
*/
public IRubyObject fill(IRubyObject[] args) {
int beg = 0;
int len = getLength();
int argc;
IRubyObject filObj;
IRubyObject begObj;
IRubyObject lenObj;
IRuby runtime = getRuntime();
ThreadContext tc = runtime.getCurrentContext();
if (tc.isBlockGiven()) {
argc = checkArgumentCount(args, 0, 2);
filObj = null;
begObj = argc > 0 ? args[0] : null;
lenObj = argc > 1 ? args[1] : null;
argc++;
} else {
argc = checkArgumentCount(args, 1, 3);
filObj = args[0];
begObj = argc > 1 ? args[1] : null;
lenObj = argc > 2 ? args[2] : null;
}
switch (argc) {
case 1 :
break;
case 2 :
if (begObj instanceof RubyRange) {
long[] begLen = ((RubyRange) begObj).getBeginLength(len, false, true);
beg = (int) begLen[0];
len = (int) begLen[1];
break;
}
/* fall through */
default :
beg = begObj.isNil() ? beg : RubyNumeric.fix2int(begObj);
if (beg < 0 && (beg += len) < 0) {
throw getRuntime().newIndexError("Negative array index");
}
len -= beg;
if (argc == 3 && !lenObj.isNil()) {
len = RubyNumeric.fix2int(lenObj);
}
}
modify();
autoExpand(beg + len);
for (int i = beg; i < beg + len; i++) {
if (filObj == null) {
list.set(i, tc.yield(runtime.newFixnum(i)));
} else {
list.set(i, filObj);
}
}
return this;
}
/** rb_ary_index
*
*/
public IRubyObject index(IRubyObject obj) {
for (int i = 0, len = getLength(); i < len; i++) {
if (obj.callMethod(getRuntime().getCurrentContext(), "==", entry(i)).isTrue()) {
return getRuntime().newFixnum(i);
}
}
return getRuntime().getNil();
}
/** rb_ary_rindex
*
*/
public IRubyObject rindex(IRubyObject obj) {
for (int i = getLength() - 1; i >= 0; i--) {
if (obj.callMethod(getRuntime().getCurrentContext(), "==", entry(i)).isTrue()) {
return getRuntime().newFixnum(i);
}
}
return getRuntime().getNil();
}
public IRubyObject indices(IRubyObject[] args) {
IRubyObject[] result = new IRubyObject[args.length];
boolean taint = false;
for (int i = 0; i < args.length; i++) {
result[i] = entry(RubyNumeric.fix2int(args[i]));
taint |= result[i].isTaint();
}
IRubyObject ary = ((ArrayMetaClass) getMetaClass()).create(result);
ary.setTaint(taint);
return ary;
}
/** rb_ary_clone
*
*/
public IRubyObject rbClone() {
RubyArray result = getRuntime().newArray(new ArrayList(list));
result.setTaint(isTaint());
result.initCopy(this);
result.setFrozen(isFrozen());
return result;
}
/** rb_ary_reverse_bang
*
*/
public IRubyObject reverse_bang() {