|
@@ -12,15 +12,26 @@ public class Node {
|
|
HashSet<Node> linkNodes;
|
|
HashSet<Node> linkNodes;
|
|
Node linkNode;
|
|
Node linkNode;
|
|
|
|
|
|
|
|
+ int hash;
|
|
|
|
+
|
|
public Node(Node p, String s) {
|
|
public Node(Node p, String s) {
|
|
this.linkNode = null;
|
|
this.linkNode = null;
|
|
this.linkNodes = new HashSet<>();
|
|
this.linkNodes = new HashSet<>();
|
|
this.venn = s;
|
|
this.venn = s;
|
|
this.parent = p;
|
|
this.parent = p;
|
|
|
|
+ this.hash = 0;
|
|
|
|
|
|
this.children = new HashSet<>();
|
|
this.children = new HashSet<>();
|
|
if (s.length() > 1)
|
|
if (s.length() > 1)
|
|
setChildren(s);
|
|
setChildren(s);
|
|
|
|
+ if (children.isEmpty()) {
|
|
|
|
+ hash = (venn.hashCode()*1073676287) ^ (venn.hashCode()*97);
|
|
|
|
+ } else {
|
|
|
|
+ for (Node child : children) {
|
|
|
|
+ hash = hash ^ child.hash;
|
|
|
|
+ }
|
|
|
|
+ hash *= 33;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public Node(Node p, char s) {
|
|
public Node(Node p, char s) {
|
|
@@ -87,11 +98,16 @@ public class Node {
|
|
}
|
|
}
|
|
|
|
|
|
public boolean equals(Object obj) {
|
|
public boolean equals(Object obj) {
|
|
- if (!(obj instanceof Node)) return false;
|
|
|
|
|
|
+ if (this == obj) return true; // Literally the same object
|
|
|
|
+ if (!(obj instanceof Node)) return false; // Wrong type
|
|
|
|
|
|
Node node = (Node) obj;
|
|
Node node = (Node) obj;
|
|
- String nodeVenn = node.getVenn();
|
|
|
|
|
|
+ if (hash != node.hash) return false; // equal object have equal hash codes
|
|
|
|
+
|
|
|
|
+ // If above checks fall through, we need to actually look at the structure
|
|
|
|
+ // Of the Node.
|
|
|
|
|
|
|
|
+ String nodeVenn = node.getVenn();
|
|
// Case for leaf nodes
|
|
// Case for leaf nodes
|
|
if ((nodeVenn.length() == 1) && (this.venn.length() == 1)) {
|
|
if ((nodeVenn.length() == 1) && (this.venn.length() == 1)) {
|
|
return nodeVenn.equals(this.venn);
|
|
return nodeVenn.equals(this.venn);
|