forked from alexbeutel/FlexiFaCT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatArray.java
91 lines (76 loc) · 1.62 KB
/
FloatArray.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
import java.io.*;
import org.apache.hadoop.io.*;
public class FloatArray implements WritableComparable<FloatArray> {
public float[] ar;
public FloatArray() { }
public FloatArray(int n) {
ar = new float[n];
}
public FloatArray(float[] a) {
ar = a;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(ar.length);
for(int i = 0; i < ar.length; i++) {
out.writeFloat(ar[i]);
}
}
@Override
public void readFields(DataInput in) throws IOException {
int n = in.readInt();
ar = new float[n];
for(int i = 0; i < ar.length; i++) {
ar[i] = in.readFloat();
}
}
@Override
public int hashCode() {
return this.toString().hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof FloatArray) {
FloatArray ia = (FloatArray) o;
if(ia.ar.length == ar.length) {
for(int i = 0; i < ar.length; i++) {
if( ia.ar[i] != ar[i] ) {
return false;
}
}
return true;
} else {
return false;
}
}
return false;
}
@Override
public String toString() {
String s = "";
for (int i = 0; i < ar.length; i++) {
if (i > 0) {
s += "\t";
}
s += ar[i];
}
return s;
}
@Override
public int compareTo(FloatArray ia) {
int cmp = compare(ar.length,ia.ar.length);
for(int i = 0; i < ar.length; i++) {
cmp = compare(ar[i],ia.ar[i]);
if (cmp != 0) {
return cmp;
}
}
return 0;
}
public static int compare(int a, int b) {
return (a < b ? -1 : (a == b ? 0 : 1));
}
public static int compare(float a, float b) {
return (a < b ? -1 : (a == b ? 0 : 1));
}
}