소스 검색

HashSet-ectomy

Caleb Fangmeier 6 년 전
부모
커밋
0cbcce3ee5
2개의 변경된 파일33개의 추가작업 그리고 31개의 파일을 삭제
  1. 7 7
      src/com/pact/Node.java
  2. 26 24
      src/com/pact/NodePair.java

+ 7 - 7
src/com/pact/Node.java

@@ -7,12 +7,12 @@ import java.util.List;
 
 public class Node {
     Node parent;
-    String venn;
-    HashSet<Node> children;
+    private String venn;
+    private List<Node> children;
     HashSet<Node> linkNodes;
-    Node linkNode;
+    private Node linkNode;
 
-    int hash;
+    private int hash;
 
     public Node(Node p, String s) {
         this.linkNode = null;
@@ -21,7 +21,7 @@ public class Node {
         this.parent = p;
         this.hash = 0;
 
-        this.children = new HashSet<>();
+        this.children = new ArrayList<>();
         if (s.length() > 1)
             setChildren(s);
         if (children.isEmpty()) {
@@ -46,7 +46,7 @@ public class Node {
                 if (vennChildren.charAt(count) == '(') {
                     int start = count;
                     count++;
-                    int end = 0;
+                    int end;
                     int brackets = 1;
                     while (true)
                     {
@@ -90,7 +90,7 @@ public class Node {
 
     public HashSet<Node> getSiblings() {
         if (this.parent != null) {
-            HashSet<Node> sibs = (HashSet)this.parent.children.clone();
+            HashSet<Node> sibs = new HashSet<>(parent.children);
             sibs.remove(this);
             return sibs;
         }

+ 26 - 24
src/com/pact/NodePair.java

@@ -1,5 +1,6 @@
 package com.pact;
 
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -37,49 +38,55 @@ public class NodePair {
     }
 
     public HashSet<Node> combineSubTrees() {
+        List<Node> res = _combineSubTrees();
+        return new HashSet(res);
+
+    }
+
+    public List<Node> _combineSubTrees() {
         System.out.println("combineSubTrees==========================");
         System.out.println("node1: " + node1.getVenn());
         System.out.println("node2: " + node2.getVenn());
         System.out.println("node1Full: " + node1.root().getVenn());
         System.out.println("node2Full: " + node2.root().getVenn());
-        HashSet<Node> commonSiblings = new HashSet<>();
+        List<Node> commonSiblings = new ArrayList<>();
 
-        HashSet<Node> node1Siblings = this.node1.getSiblings();
+        List<Node> node1Siblings = new ArrayList<>(this.node1.getSiblings());
         Iterator it1 = node1Siblings.iterator();
-        HashSet<Node> node2Siblings = this.node2.getSiblings();
+        List<Node> node2Siblings = new ArrayList<>(this.node2.getSiblings());
         Iterator it2 = node2Siblings.iterator();
 
-        String siblingsVenn = "";
+        StringBuilder siblingsVenn = new StringBuilder();
 
         if (!node1Siblings.isEmpty()) {
-            siblingsVenn = siblingsVenn + ((Node)it2.next()).getVenn();
+            siblingsVenn.append(((Node) it2.next()).getVenn());
         } else {
             if (it1.hasNext()) {
-                siblingsVenn = siblingsVenn + ((Node)it1.next()).getVenn();
+                siblingsVenn.append(((Node) it1.next()).getVenn());
                 if (it1.hasNext()) {
-                    String temp = siblingsVenn;
-                    siblingsVenn = "(" + temp;
+                    String temp = siblingsVenn.toString();
+                    siblingsVenn = new StringBuilder("(" + temp);
                     while (it1.hasNext()) {
-                        siblingsVenn = siblingsVenn + ((Node)it1.next()).getVenn();
+                        siblingsVenn.append(((Node) it1.next()).getVenn());
                     }
-                    siblingsVenn = siblingsVenn + ")";
+                    siblingsVenn.append(")");
                 }
-                commonSiblings.add(new Node(null, siblingsVenn));
+                commonSiblings.add(new Node(null, siblingsVenn.toString()));
                 return commonSiblings;
             }
             return commonSiblings;
         }
 
         if (it2.hasNext()) {
-            String temp = siblingsVenn;
-            siblingsVenn = "(" + temp;
+            String temp = siblingsVenn.toString();
+            siblingsVenn = new StringBuilder("(" + temp);
             while (it2.hasNext()) {
-                siblingsVenn = siblingsVenn + ((Node)it2.next()).getVenn();
+                siblingsVenn.append(((Node) it2.next()).getVenn());
             }
-            siblingsVenn = siblingsVenn + ")";
+            siblingsVenn.append(")");
         }
 
-        Node nodesibs1 = new Node(null, siblingsVenn);
+        Node nodesibs1 = new Node(null, siblingsVenn.toString());
         if (!it1.hasNext()) {
             commonSiblings.add(nodesibs1);
             return commonSiblings;
@@ -98,16 +105,11 @@ public class NodePair {
         }
 
         Node nodeSibs2 = new Node(null, siblings2);
-        HashSet<Node> result = Node.combine(nodesibs1, nodeSibs2);
-        System.out.println("result: ");
-        for(Node n : result){
-            System.out.println("\t " + n.getVenn());
-        }
-        return result;
+        return new ArrayList(Node.combine(nodesibs1, nodeSibs2));
     }
 
     protected HashSet<NodePair> combineNew() {
-        HashSet<NodePair> setResult = new HashSet<>();
+        List<NodePair> setResult = new ArrayList<>();
         Node resultNode1 = new Node(null, this.node2.root().getVenn());
         String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
 
@@ -116,7 +118,7 @@ public class NodePair {
             Node resultNode2 = new Node(null, result);
             setResult.add(new NodePair(resultNode1, resultNode2));
         }
-        return setResult;
+        return new HashSet(setResult);
     }
 
     private String buildUp(Node n, String s) {