-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.java
62 lines (53 loc) · 1.6 KB
/
Vertex.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
/** Class to represent a vertex (in a graph).
*/
public class Vertex implements Comparable<Vertex> {
/* Note that the nextID variable had to be moved to the graph class. */
/** Vertex unique ID number. */
private int num;
/** Data stored in the vertex. */
private Object data;
/** Create a new vertex.
* @param d the data to store in the node
* @param id the unique id of the node
*/
public Vertex(Object d, int id) {
this.data = d;
this.num = id;
}
/** Get the id of this vertex.
* @return the id
*/
public int id() {
return this.num;
}
/** Get a string representation of the vertex.
* @return the string
*/
public String toString() {
return this.num + "";
}
/** Check if two vertices are the same based on ID.
* @param other the vertex to compare to this
* @return true if the same, false otherwise
*/
public boolean equals(Object other) {
if (other instanceof Vertex) {
Vertex v = (Vertex) other;
return this.num == v.num; // want these to be unique
}
return false;
}
/** Get the hashcode of a vertex based on its ID.
* @return the hashcode
*/
public int hashCode() {
return (new Integer(this.num)).hashCode();
}
/** Compare two vertices based on their IDs.
* @param other the vertex to compare to this
* @return negative if this < other, 0 if equal, positive if this > other
*/
public int compareTo(Vertex other) {
return this.num - other.num;
}
}