forked from Anuken/Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructs.java
More file actions
318 lines (266 loc) · 9.24 KB
/
Structs.java
File metadata and controls
318 lines (266 loc) · 9.24 KB
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
package arc.util;
import arc.func.*;
import arc.math.*;
import arc.struct.*;
import java.util.*;
public class Structs{
public static boolean eq(Object a, Object b){
return (a == b) || (a != null && a.equals(b));
}
@SafeVarargs
public static <T> T[] arr(T... array){
return array;
}
/** Remove all values that match this predicate. */
public static <T> void filter(Iterable<T> iterable, Boolf<T> removal){
filter(iterable.iterator(), removal);
}
/** Remove all values that match this predicate. */
public static <T> void filter(Iterator<T> it, Boolf<T> removal){
while(it.hasNext()){
if(removal.get(it.next())) it.remove();
}
}
public static <T> T[] filter(Class<T> type, T[] array, Boolf<T> value){
Seq<T> out = new Seq<>(true, array.length, type);
for(T t : array){
if(value.get(t)) out.add(t);
}
return out.toArray();
}
@SafeVarargs
public static <T> T random(T... array){
if(array.length == 0) return null;
return array[Mathf.random(array.length - 1)];
}
@SafeVarargs
public static <T> T random(Rand rand, T... array){
if(array.length == 0) return null;
return array[rand.random(array.length - 1)];
}
public static <T> int count(T[] array, Boolf<T> value){
int total = 0;
for(T t : array){
if(value.get(t)) total ++;
}
return total;
}
public static <T> int max(Iterable<T> list, Intf<T> intifier){
boolean first = true;
int index = 0;
for(T i : list){
int s = intifier.get(i);
if(first) index = s;
else if(s > index) index = s;
first = false;
}
return index;
}
public static <T> int max(T[] list, Intf<T> intifier){
boolean first = true;
int index = 0;
for(T i : list){
int s = intifier.get(i);
if(first) index = s;
else if(s > index) index = s;
first = false;
}
return index;
}
public static <T> int min(Iterable<T> list, Intf<T> intifier){
boolean first = true;
int index = 0;
for(T i : list){
int s = intifier.get(i);
if(first) index = s;
else if(s < index) index = s;
first = false;
}
return index;
}
public static <T> int min(T[] list, Intf<T> intifier){
boolean first = true;
int index = 0;
for(T i : list){
int s = intifier.get(i);
if(first) index = s;
else if(s < index) index = s;
first = false;
}
return index;
}
/**Uses identity comparisons.*/
public static <T> boolean contains(T[] array, T value){
for(T t : array){
if(t == value || (value != null && value.equals(t))) return true;
}
return false;
}
public static <T> boolean contains(T[] array, Boolf<T> value){
return find(array, value) != null;
}
public static <T> boolean contains(Iterable<T> array, Boolf<T> value){
return find(array, value) != null;
}
public static <T> T find(T[] array, Boolf<T> value){
for(T t : array){
if(value.get(t)) return t;
}
return null;
}
public static <T> T find(Iterable<T> array, Boolf<T> value){
for(T t : array){
if(value.get(t)) return t;
}
return null;
}
public static <T> int indexOf(T[] array, T value){
for(int i = 0; i < array.length; i++){
if(array[i] == value) return i;
}
return -1;
}
public static <T> int indexOf(T[] array, Boolf<T> value){
for(int i = 0; i < array.length; i++){
if(value.get(array[i])) return i;
}
return -1;
}
public static <T> int indexOf(Iterable<T> array, Boolf<T> value){
int i = 0;
for(T t : array){
if(value.get(t)) return i;
i ++;
}
return -1;
}
public static <T> T[] remove(T[] array, T value){
return remove(array, indexOf(array, value));
}
public static <T> T[] remove(T[] array, int index){
if(index < 0 || index >= array.length){
return array;
}
T[] next = Reflect.newArray(array, array.length - 1);
System.arraycopy(array, 0, next, 0, index);
if(index < array.length - 1){
System.arraycopy(array, index + 1, next, index, array.length - 1 - index);
}
return next;
}
public static <T> T[] add(T[] array, T item){
T[] next = Reflect.newArray(array, array.length + 1);
next[array.length] = item;
System.arraycopy(array, 0, next, 0, array.length);
return next;
}
public static <T> void swap(T[] array, int a, int b){
T temp = array[a];
array[a] = array[b];
array[b] = temp;
}
/** Equivalent to Comparator#thenComparsing, but more compatible. */
public static <T> Comparator<T> comps(Comparator<T> first, Comparator<T> second){
return (a, b) -> {
int value = first.compare(a, b);
return value != 0 ? value : second.compare(a, b);
};
}
public static <T, U> Comparator<T> comparing(Func<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator){
return (c1, c2) -> keyComparator.compare(keyExtractor.get(c1), keyExtractor.get(c2));
}
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Func<? super T, ? extends U> keyExtractor){
return (c1, c2) -> keyExtractor.get(c1).compareTo(keyExtractor.get(c2));
}
public static <T> Comparator<T> comparingFloat(Floatf<? super T> keyExtractor){
return (c1, c2) -> Float.compare(keyExtractor.get(c1), keyExtractor.get(c2));
}
public static <T> Comparator<T> comparingInt(Intf<? super T> keyExtractor){
return (c1, c2) -> Integer.compare(keyExtractor.get(c1), keyExtractor.get(c2));
}
public static <T> Comparator<T> comparingLong(Longf<? super T> keyExtractor){
return (c1, c2) -> Long.compare(keyExtractor.get(c1), keyExtractor.get(c2));
}
public static <T> Comparator<T> comparingBool(Boolf<? super T> keyExtractor){
return (c1, c2) -> Boolean.compare(keyExtractor.get(c1), keyExtractor.get(c2));
}
@SafeVarargs
public static <T> void each(Cons<T> cons, T... objects){
for(T t : objects){
cons.get(t);
}
}
/** Alias of {@link #forEach}. */
public static <T> void each(Iterable<T> i, Cons<T> cons){
forEach(i, cons);
}
public static <T> void forEach(Iterable<T> i, Cons<T> cons){
for(T t : i){
cons.get(t);
}
}
public static <T> T findMin(T[] arr, Comparator<T> comp){
T result = null;
for(T t : arr){
if(result == null || comp.compare(result, t) < 0){
result = t;
}
}
return result;
}
public static <T> T findMin(T[] arr, Floatf<T> proc){
T result = null;
float min = Float.MAX_VALUE;
for(T t : arr){
float val = proc.get(t);
if(val <= min){
result = t;
min = val;
}
}
return result;
}
public static <T> T findMin(Iterable<T> arr, Comparator<T> comp){
T result = null;
for(T t : arr){
if(result == null || comp.compare(result, t) < 0){
result = t;
}
}
return result;
}
public static <T> T findMin(Iterable<T> arr, Boolf<T> allow, Comparator<T> comp){
T result = null;
for(T t : arr){
if(allow.get(t) && (result == null || comp.compare(result, t) < 0)){
result = t;
}
}
return result;
}
public static <T> boolean inBounds(int x, int y, T[][] array){
return x >= 0 && y >= 0 && x < array.length && y < array[0].length;
}
public static boolean inBounds(int x, int y, int[][] array){
return x >= 0 && y >= 0 && x < array.length && y < array[0].length;
}
public static boolean inBounds(int x, int y, float[][] array){
return x >= 0 && y >= 0 && x < array.length && y < array[0].length;
}
public static boolean inBounds(int x, int y, boolean[][] array){
return x >= 0 && y >= 0 && x < array.length && y < array[0].length;
}
public static <T> boolean inBounds(int x, int y, int z, T[][][] array){
return x >= 0 && y >= 0 && z >= 0 && x < array.length && y < array[0].length && z < array[0][0].length;
}
public static boolean inBounds(int x, int y, int z, int[][][] array){
return x >= 0 && y >= 0 && z >= 0 && x < array.length && y < array[0].length && z < array[0][0].length;
}
public static boolean inBounds(int x, int y, int z, int size, int padding){
return x >= padding && y >= padding && z >= padding && x < size - padding && y < size - padding
&& z < size - padding;
}
public static boolean inBounds(int x, int y, int width, int height){
return x >= 0 && y >= 0 && x < width && y < height;
}
}