-
Notifications
You must be signed in to change notification settings - Fork 1
/
TriangularMatrix.pde
58 lines (49 loc) · 1.15 KB
/
TriangularMatrix.pde
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
/**
* A triangular matrix single-array representation without the diagonal.
* useful for storing symmetrical relationships, like distance, etc.
*/
class TriangularMatrix<E> {
ArrayList<E> values;
TriangularMatrix(int size) {
int s = (size - 1) * size / 2;
values = new ArrayList<E>(s);
// populate the array list to the specified size
while (s-- > 0) {
values.add(null);
}
}
int calcIndex(int x, int y) {
// make sure x > y
if (x < y) {
int i = x;
x = y;
y = i;
}
return x * (x - 1) / 2 + y;
}
/**
* Set a value between two indexes
*
* @param x one of the indexes
* @param y the other index
* @param value the value to set
* @return the old value for the index
*/
E set(int x, int y, E value) {
int ix = calcIndex(x, y);
E ret = values.get(ix);
values.set(ix, value);
return ret;
}
/**
* Get a value for two indexes
*
* @param x one of the indexes
* @param y the other index
* @return the value for the index
*/
E get(int x, int y) {
int ix = calcIndex(x, y);
return values.get(ix);
}
};