Skip to content

Commit 9371c5b

Browse files
committed
Replaced Array.types static fake enums with real enums.
1 parent f09ad5e commit 9371c5b

5 files changed

Lines changed: 87 additions & 51 deletions

File tree

com/arrayfire/Array.java

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,60 @@
22

33
public class Array extends ArrayFire implements AutoCloseable {
44

5-
public static final int FloatType = 0;
6-
public static final int FloatComplexType = 1;
7-
public static final int DoubleType = 2;
8-
public static final int DoubleComplexType = 3;
9-
public static final int BooleanType = 4;
10-
public static final int IntType = 5;
5+
public enum Type {
6+
Float(0),
7+
FloatComplex(1),
8+
Double(2),
9+
DoubleComplex(3),
10+
Boolean(4),
11+
Int(5);
12+
13+
private final int type;
14+
15+
private Type(int type) {
16+
this.type = type;
17+
}
18+
19+
public static Type fromInt(int type) throws Exception {
20+
switch (type) {
21+
case 0:
22+
return Type.Float;
23+
case 1:
24+
return Type.FloatComplex;
25+
case 2:
26+
return Type.Double;
27+
case 3:
28+
return Type.DoubleComplex;
29+
case 4:
30+
return Type.Boolean;
31+
case 5:
32+
return Type.Int;
33+
default:
34+
throw new Exception("Unknown type.");
35+
}
36+
}
37+
38+
public int getType() {
39+
return type;
40+
}
41+
42+
public Type f32() {
43+
return Type.Float;
44+
}
45+
46+
public Type f64() {
47+
return Type.Double;
48+
}
49+
50+
public Type int32() {
51+
return Type.Int;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return this.name();
57+
}
58+
}
1159

1260
private native static void destroyArray(long ref);
1361

@@ -152,20 +200,8 @@ public int[] dims() {
152200
return getDims(ref);
153201
}
154202

155-
public int type() {
156-
return getType(ref);
157-
}
158-
159-
public String typeName(int ty) throws Exception {
160-
switch (ty) {
161-
case FloatType: return "float";
162-
case DoubleType: return "double";
163-
case IntType: return "int";
164-
case BooleanType: return "boolean";
165-
case FloatComplexType: return "FloatComplex";
166-
case DoubleComplexType: return "DoubleComplex";
167-
default: throw new Exception("Unknown type");
168-
}
203+
public Array.Type type() throws Exception {
204+
return Array.Type.fromInt(getType(ref));
169205
}
170206

171207
protected static int[] dim4(int[] dims) throws Exception {
@@ -181,14 +217,14 @@ protected static int[] dim4(int[] dims) throws Exception {
181217
return adims;
182218
}
183219

184-
protected void assertType(int ty) throws Exception {
220+
protected void assertType(Array.Type ty) throws Exception {
185221

186-
int myType = type();
222+
Type myType = type();
187223

188224
if (myType != ty) {
189225
String str = "Type mismatch: ";
190-
str = str + "Requested " + typeName(ty);
191-
str = str + ". Found " + typeName(myType);
226+
str = str + "Requested " + ty;
227+
str = str + ". Found " + myType;
192228
throw new Exception(str);
193229
}
194230
return;

com/arrayfire/Data.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,58 @@ public class Data extends ArrayFire {
2323
private native static long createIdentityArray(int[] dims, int type);
2424

2525
public static float[] getFloatArray(Array A) throws Exception {
26-
A.assertType(Array.FloatType);
26+
A.assertType(Array.Type.Float);
2727
return getFloatFromArray(A.ref);
2828
}
2929

3030
public static double[] getDoubleArray(Array A) throws Exception {
31-
A.assertType(Array.DoubleType);
31+
A.assertType(Array.Type.Double);
3232
return getDoubleFromArray(A.ref);
3333
}
3434

3535
public static FloatComplex[] getFloatComplexArray(Array A) throws Exception {
36-
A.assertType(Array.FloatComplexType);
36+
A.assertType(Array.Type.FloatComplex);
3737
return getFloatComplexFromArray(A.ref);
3838
}
3939

4040
public static DoubleComplex[] getDoubleComplexArray(Array A) throws Exception {
41-
A.assertType(Array.DoubleComplexType);
41+
A.assertType(Array.Type.DoubleComplex);
4242
return getDoubleComplexFromArray(A.ref);
4343
}
4444

4545
public static int[] getIntArray(Array A) throws Exception {
46-
A.assertType(Array.IntType);
46+
A.assertType(Array.Type.Int);
4747
return getIntFromArray(A.ref);
4848
}
4949

5050
public static boolean[] getBooleanArray(Array A) throws Exception {
51-
A.assertType(Array.BooleanType);
51+
A.assertType(Array.Type.Boolean);
5252
return getBooleanFromArray(A.ref);
5353
}
5454

5555
// Binary operations
56-
public static void randu(Array res, int[] dims, int type) throws Exception {
56+
public static void randu(Array res, int[] dims, Array.Type type) throws Exception {
5757
int[] adims = Array.dim4(dims);
58-
res.set(createRanduArray(adims, type));
58+
res.set(createRanduArray(adims, type.getType()));
5959
}
6060

61-
public static void randn(Array res, int[] dims, int type) throws Exception {
61+
public static void randn(Array res, int[] dims, Array.Type type) throws Exception {
6262
int[] adims = Array.dim4(dims);
63-
res.set(createRandnArray(adims, type));
63+
res.set(createRandnArray(adims, type.getType()));
6464
}
6565

66-
public static void constant(Array res, double val, int[] dims, int type) throws Exception {
66+
public static void constant(Array res, double val, int[] dims, Array.Type type) throws Exception {
6767
int[] adims = Array.dim4(dims);
68-
res.set(createConstantsArray(val, adims, type));
68+
res.set(createConstantsArray(val, adims, type.getType()));
6969
}
7070

71-
public static void identity(Array res, int[] dims, int type) throws Exception {
71+
public static void identity(Array res, int[] dims, Array.Type type) throws Exception {
7272
int[] adims = Array.dim4(dims);
73-
res.set(createIdentityArray(adims, type));
73+
res.set(createIdentityArray(adims, type.getType()));
7474
}
7575

7676
public static void identity(Array res, int[] dims) throws Exception {
77-
identity(res, dims, Array.FloatType);
77+
identity(res, dims, Array.Type.Float);
7878
}
7979

8080
}

examples/HelloWorld.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String[] args) {
1111
Util.info();
1212

1313
System.out.println("Create a 5-by-3 matrix of random floats on the GPU");
14-
Data.randu(a, new int[] { 5, 3 }, Array.FloatType);
14+
Data.randu(a, new int[] { 5, 3 }, Array.Type.Float);
1515
System.out.println(a.toString("a"));
1616

1717
System.out.println("Element-wise arithmetic");
@@ -29,8 +29,8 @@ public static void main(String[] args) {
2929
System.out.println("Calculate weighted variance.");
3030
Array forVar = new Array();
3131
Array weights = new Array();
32-
Data.randn(forVar, new int[] { 5, 5 }, Array.DoubleType);
33-
Data.randn(weights, new int[] { 5, 5 }, Array.DoubleType);
32+
Data.randn(forVar, new int[] { 5, 5 }, Array.Type.Double);
33+
Data.randn(weights, new int[] { 5, 5 }, Array.Type.Double);
3434
System.out.println(forVar.toString("forVar"));
3535

3636
double abc = Statistics.var(forVar, weights, Double.class);
@@ -40,15 +40,15 @@ public static void main(String[] args) {
4040

4141
System.out.println("Median");
4242
Array forMedian = new Array();
43-
Data.randu(forMedian, new int[] { 3, 5 }, Array.DoubleType);
43+
Data.randu(forMedian, new int[] { 3, 5 }, Array.Type.Double);
4444
System.out.println(forMedian.toString("forMedian"));
4545
double median = Statistics.median(forMedian, Double.class);
4646
System.out.printf("Median = %f\n", median);
4747
forMedian.close();
4848

4949
System.out.println("Calculate standard deviation");
5050
Array forStdev = new Array();
51-
Data.randu(forStdev, new int[] { 5, 3 }, Array.DoubleType);
51+
Data.randu(forStdev, new int[] { 5, 3 }, Array.Type.Double);
5252
System.out.println(forStdev.toString("forStdev"));
5353
double stdev = Statistics.stdev(forStdev, Double.class);
5454
System.out.println(String.format("Stdev is: %f", stdev));
@@ -57,8 +57,8 @@ public static void main(String[] args) {
5757
System.out.println("Covariance");
5858
Array x = new Array();
5959
Array z = new Array();
60-
Data.randu(x, new int[] { 5, 3 }, Array.DoubleType);
61-
Data.randu(z, new int[] { 5, 3 }, Array.DoubleType);
60+
Data.randu(x, new int[] { 5, 3 }, Array.Type.Double);
61+
Data.randu(z, new int[] { 5, 3 }, Array.Type.Double);
6262
System.out.println(x.toString("x"));
6363
System.out.println(z.toString("z"));
6464
Array cov = Statistics.cov(x, z, false);
@@ -72,7 +72,7 @@ public static void main(String[] args) {
7272

7373
System.out.println("Topk");
7474
Array forTopk = new Array();
75-
Data.randu(forTopk, new int[] { 3, 3 }, Array.DoubleType);
75+
Data.randu(forTopk, new int[] { 3, 3 }, Array.Type.Double);
7676
System.out.println(forTopk.toString("forTopk"));
7777
Array[] results = Statistics.topk(forTopk, 3, 0, Statistics.TopkOrder.DEFAULT);
7878
System.out.println(results[0].toString("Indicies"));
@@ -96,7 +96,7 @@ public static void main(String[] args) {
9696

9797
System.out.println("Add e and random array");
9898
Array randa = new Array();
99-
Data.randu(randa, dims, Array.FloatType);
99+
Data.randu(randa, dims, Array.Type.Float);
100100
Arith.add(f, e, randa);
101101
System.out.println(f.toString("f"));
102102

examples/MonteCarloPi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public static double deviceCalcPi(int size) throws Exception {
2222
try {
2323

2424
int[] dims = new int[] { size, 1 };
25-
Data.randu(x, dims, Array.FloatType);
26-
Data.randu(y, dims, Array.FloatType);
25+
Data.randu(x, dims, Array.Type.Float);
26+
Data.randu(y, dims, Array.Type.Float);
2727

2828
Arith.mul(x, x, x);
2929
Arith.mul(y, y, y);

examples/WindowEx.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public static void main(String[] args) {
55
System.out.println("Creating window");
66
try {
77
Array img = new Array();
8-
Data.randu(img, new int[] { 200, 200 }, Array.IntType);
8+
Data.randu(img, new int[] { 200, 200 }, Array.Type.Int);
99
Window window = new Window();
1010
while (!window.closeWindow()) {
1111
window.image(img, "Image");

0 commit comments

Comments
 (0)