-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.java
45 lines (41 loc) · 1.32 KB
/
Path.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
public class Path {
public int indices[];
public Path( int startIndices[] ) {
indices = startIndices;
}
public static Path join( Path paths[] ) {
int resultLength = 0;
for ( int i = 0; i < paths.length; i++ ) {
resultLength += paths[i].numUniqueIndices();
}
int resultIndices[] = new int[resultLength];
int previousIndex = -1; // No real index should ever be -1.
int k = 0;
for ( int i = 0; i < paths.length; i++ ) {
for ( int j = 0; j < paths[i].indices.length; j++ ) {
if ( paths[i].indices[j] != previousIndex ) {
resultIndices[k] = paths[i].indices[j];
k++;
}
previousIndex = paths[i].indices[j];
}
}
return new Path( resultIndices );
}
public Path reverse() {
int numElements = indices.length;
int reversed[] = new int[numElements];
for ( int i = 0; i < numElements; i++ ) {
reversed[numElements - 1 - i] = indices[i];
}
return new Path( reversed );
}
private int numUniqueIndices() {
if ( indices.length == 2 ) {
if ( indices[0] == indices[1] ) {
return 1;
}
}
return indices.length;
}
}