1
1
package com .badlogic .gdx .utils ;
2
2
3
- import com .badlogic .gdx .function .BooleanFunction ;
4
-
5
3
import javax .annotation .Nonnull ;
6
4
import java .util .*;
7
5
import java .util .function .Consumer ;
6
+ import java .util .function .Predicate ;
8
7
9
8
/**
10
9
* Array utility functions.
@@ -28,9 +27,9 @@ public static <T> void forEach(T[] array, Consumer<T> action) {
28
27
* <p>
29
28
* @return true if end of array was reached
30
29
*/
31
- public static <T > boolean forEachWhile (T [] array , BooleanFunction <T > action ) {
30
+ public static <T > boolean forEachWhile (T [] array , Predicate <T > predicate ) {
32
31
for (T t : array ) {
33
- if (!action . apply (t )) {
32
+ if (!predicate . test (t )) {
34
33
return false ;
35
34
}
36
35
}
@@ -42,9 +41,9 @@ public static <T> boolean forEachWhile(T[] array, BooleanFunction<T> action) {
42
41
* <p>
43
42
* @return true if end of array was reached
44
43
*/
45
- public static <T > boolean forEachWhile (Array <T > array , BooleanFunction <T > action ) {
44
+ public static <T > boolean forEachWhile (Array <T > array , Predicate <T > action ) {
46
45
for (T t : array ) {
47
- if (!action .apply (t )) {
46
+ if (!action .test (t )) {
48
47
return false ;
49
48
}
50
49
}
@@ -56,9 +55,9 @@ public static <T> boolean forEachWhile(Array<T> array, BooleanFunction<T> action
56
55
* <p>
57
56
* Returns null if no match is found.
58
57
*/
59
- public static <T > T find (T [] array , BooleanFunction <T > match ) {
58
+ public static <T > T find (T [] array , Predicate <T > match ) {
60
59
for (T t : array ) {
61
- if (match .apply (t )) {
60
+ if (match .test (t )) {
62
61
return t ;
63
62
}
64
63
}
@@ -70,9 +69,9 @@ public static <T> T find(T[] array, BooleanFunction<T> match) {
70
69
* <p>
71
70
* Returns null if no match is found.
72
71
*/
73
- public static <T > T find (Array <T > array , BooleanFunction <T > match ) {
72
+ public static <T > T find (Array <T > array , Predicate <T > match ) {
74
73
for (T t : array ) {
75
- if (match .apply (t )) {
74
+ if (match .test (t )) {
76
75
return t ;
77
76
}
78
77
}
@@ -84,9 +83,9 @@ public static <T> T find(Array<T> array, BooleanFunction<T> match) {
84
83
* <p>
85
84
* Returns -1 if no match is found.
86
85
*/
87
- public static <T > int findIndex (T [] array , BooleanFunction <T > match ) {
86
+ public static <T > int findIndex (T [] array , Predicate <T > match ) {
88
87
for (int i = 0 ; i < array .length ; i ++) {
89
- if (match .apply (array [i ])) {
88
+ if (match .test (array [i ])) {
90
89
return i ;
91
90
}
92
91
}
@@ -98,9 +97,9 @@ public static <T> int findIndex(T[] array, BooleanFunction<T> match) {
98
97
* <p>
99
98
* Returns -1 if no match is found.
100
99
*/
101
- public static <T > int findIndex (Array <T > array , BooleanFunction <T > match ) {
100
+ public static <T > int findIndex (Array <T > array , Predicate <T > match ) {
102
101
for (int i = 0 ; i < array .size ; i ++) {
103
- if (match .apply (array .get (i ))) {
102
+ if (match .test (array .get (i ))) {
104
103
return i ;
105
104
}
106
105
}
@@ -110,9 +109,9 @@ public static <T> int findIndex(Array<T> array, BooleanFunction<T> match) {
110
109
/**
111
110
* Iterates the array, consuming items only which fulfill the user-defined comparison.
112
111
*/
113
- public static <T > void findAll (T [] array , BooleanFunction <T > match , Consumer <T > action ) {
112
+ public static <T > void findAll (T [] array , Predicate <T > match , Consumer <T > action ) {
114
113
for (T t : array ) {
115
- if (match .apply (t )) {
114
+ if (match .test (t )) {
116
115
action .accept (t );
117
116
}
118
117
}
@@ -179,6 +178,40 @@ public static <T> T[][] expand(T[][] arrayOfArray, Class<T> clazz,
179
178
return dest ;
180
179
}
181
180
181
+ /**
182
+ * Expands an existing three-dimensional array.
183
+ * <p>
184
+ * This is a very memory-inefficient operation.
185
+ */
186
+ @ SuppressWarnings ("unchecked" )
187
+ public static <T > T [][][] expand (T [][][] arrayOfArrayOfArray , Class <T > clazz ,
188
+ int lowerDim0 , int upperDim0 , int lowerDim1 , int upperDim1 ) {
189
+
190
+ int columns = arrayOfArrayOfArray .length + lowerDim0 + upperDim0 ;
191
+ int rows = arrayOfArrayOfArray [0 ].length + lowerDim1 + upperDim1 ;
192
+ int depth = arrayOfArrayOfArray [0 ][0 ].length ;
193
+
194
+ Object copy = java .lang .reflect .Array .newInstance (clazz , columns , rows , depth );
195
+
196
+ T [][][] dest = (T [][][]) copy ;
197
+
198
+ for (int i = lowerDim0 ; i < columns - upperDim0 ; i ++) {
199
+ for (int j = lowerDim1 ; j < rows - upperDim1 ; j ++) {
200
+ /*for (int k = 0; k < depth; k++) {
201
+ dest[i][j][k] = arrayOfArrayOfArray[i - lowerDim0][j - lowerDim1][k];
202
+ }*/
203
+ System .arraycopy (
204
+ arrayOfArrayOfArray [i - lowerDim0 ][j - lowerDim1 ],
205
+ 0 ,
206
+ dest [i ][j ],
207
+ 0 ,
208
+ depth );
209
+ }
210
+ }
211
+
212
+ return dest ;
213
+ }
214
+
182
215
/**
183
216
* Combined check for null or empty array.
184
217
*/
0 commit comments