/
ArrayUtils.java
324 lines (257 loc) · 9.97 KB
/
ArrayUtils.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
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.util;
import com.oracle.truffle.api.CompilerAsserts;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyArguments;
import java.lang.reflect.Array;
import java.util.Arrays;
public abstract class ArrayUtils {
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
public static int[] extractRange(int[] source, int start, int end) {
assert checkExtractRangeArgs(source, start, end);
int length = end - start;
int[] result = new int[length];
System.arraycopy(source, start, result, 0, length);
return result;
}
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
public static long[] extractRange(long[] source, int start, int end) {
assert checkExtractRangeArgs(source, start, end);
int length = end - start;
long[] result = new long[length];
System.arraycopy(source, start, result, 0, length);
return result;
}
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
public static double[] extractRange(double[] source, int start, int end) {
assert checkExtractRangeArgs(source, start, end);
int length = end - start;
double[] result = new double[length];
System.arraycopy(source, start, result, 0, length);
return result;
}
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
public static Object[] extractRange(Object[] source, int start, int end) {
assert checkExtractRangeArgs(source, start, end);
int length = end - start;
Object[] result = new Object[length];
System.arraycopy(source, start, result, 0, length);
return result;
}
private static boolean checkExtractRangeArgs(Object source, int start, int end) {
assert source != null;
assert start >= 0;
assert start <= Array.getLength(source);
assert end >= start;
assert end <= Array.getLength(source);
return true;
}
public static boolean contains(int[] array, int value) {
for (int n = 0; n < array.length; n++) {
if (array[n] == value) {
return true;
}
}
return false;
}
public static boolean contains(long[] array, long value) {
for (int n = 0; n < array.length; n++) {
if (array[n] == value) {
return true;
}
}
return false;
}
public static boolean contains(double[] array, double value) {
for (int n = 0; n < array.length; n++) {
if (array[n] == value) {
return true;
}
}
return false;
}
public static boolean contains(Object[] array, int length, Object value) {
for (int n = 0; n < length; n++) {
if (array[n].equals(value)) {
return true;
}
}
return false;
}
public static Object[] box(int[] unboxed) {
return box(unboxed, 0);
}
public static Object[] box(long[] unboxed) {
return box(unboxed, 0);
}
public static Object[] box(double[] unboxed) {
return box(unboxed, 0);
}
public static Object[] box(Object array) {
return box(array, 0);
}
public static Object[] box(int[] unboxed, int extra) {
final Object[] boxed = new Object[unboxed.length + extra];
for (int n = 0; n < unboxed.length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] box(long[] unboxed, int extra) {
final Object[] boxed = new Object[unboxed.length + extra];
for (int n = 0; n < unboxed.length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] box(double[] unboxed, int extra) {
final Object[] boxed = new Object[unboxed.length + extra];
for (int n = 0; n < unboxed.length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] boxUntil(int[] unboxed, int length) {
final Object[] boxed = new Object[length];
for (int n = 0; n < length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] boxUntil(long[] unboxed, int length) {
final Object[] boxed = new Object[length];
for (int n = 0; n < length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] boxUntil(double[] unboxed, int length) {
final Object[] boxed = new Object[length];
for (int n = 0; n < length; n++) {
boxed[n] = unboxed[n];
}
return boxed;
}
public static Object[] box(Object array, int extra) {
CompilerAsserts.neverPartOfCompilation();
if (array == null) {
return new Object[extra];
} if (array instanceof int[]) {
return box((int[]) array, extra);
} else if (array instanceof long[]) {
return box((long[]) array, extra);
} else if (array instanceof double[]) {
return box((double[]) array, extra);
} else if (array instanceof Object[]) {
final Object[] objectArray = (Object[]) array;
return Arrays.copyOf(objectArray, objectArray.length + extra);
} else {
throw new UnsupportedOperationException();
}
}
public static int[] unboxInteger(Object[] unboxed, int length) {
CompilerAsserts.neverPartOfCompilation();
final int[] boxed = new int[length];
for (int n = 0; n < length; n++) {
boxed[n] = (int) unboxed[n];
}
return boxed;
}
public static long[] unboxLong(Object[] unboxed, int length) {
CompilerAsserts.neverPartOfCompilation();
final long[] boxed = new long[length];
for (int n = 0; n < length; n++) {
final Object value = unboxed[n];
if (value instanceof Integer) {
boxed[n] = (long) (int) unboxed[n];
} else if (value instanceof Long) {
boxed[n] = (long) unboxed[n];
}
}
return boxed;
}
public static double[] unboxDouble(Object[] unboxed, int length) {
CompilerAsserts.neverPartOfCompilation();
final double[] boxed = new double[length];
for (int n = 0; n < length; n++) {
boxed[n] = (double) unboxed[n];
}
return boxed;
}
public static void copy(Object source, Object[] destination, int destinationStart, int length) {
RubyNode.notDesignedForCompilation();
if (length == 0) {
return;
}
if (source instanceof int[]) {
final int[] unboxedSource = (int[]) source;
for (int n = 0; n < length; n++) {
destination[destinationStart + n] = unboxedSource[n];
}
} else if (source instanceof long[]) {
final long[] unboxedSource = (long[]) source;
for (int n = 0; n < length; n++) {
destination[destinationStart + n] = unboxedSource[n];
}
} else if (source instanceof double[]) {
final double[] unboxedSource = (double[]) source;
for (int n = 0; n < length; n++) {
destination[destinationStart + n] = unboxedSource[n];
}
} else if (source instanceof Object[]) {
RubyArguments.arraycopy((Object[]) source, 0, destination, destinationStart, length);
} else {
throw new UnsupportedOperationException();
}
}
public static long[] longCopyOf(int[] ints) {
final long[] longs = new long[ints.length];
for (int n = 0; n < ints.length; n++) {
longs[n] = ints[n];
}
return longs;
}
public static int capacity(int current, int needed) {
if (needed < 16) {
return 16;
} else {
int capacity = current;
while (capacity < needed) {
capacity *= 2;
}
return capacity;
}
}
}