Selaa lähdekoodia

Start of HashSet-ectomy

Caleb Fangmeier 6 vuotta sitten
vanhempi
commit
4887b86c9e
3 muutettua tiedostoa jossa 16 lisäystä ja 17 poistoa
  1. 8 7
      src/com/pact/Main.java
  2. 6 8
      src/com/pact/Node.java
  3. 2 2
      src/com/pact/test/NodeTest.java

+ 8 - 7
src/com/pact/Main.java

@@ -1,31 +1,32 @@
 package com.pact;
 
 import java.io.*;
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.List;
 
 public class Main {
 
-    public HashSet<Node> process(String in)
+    public List<Node> process(String in)
             throws IOException, BracketException {
         BufferedReader inB = new BufferedReader(new FileReader(in));
         String line;
-        HashSet<Node> templates = null;
+        List<Node> templates = null;
 
         // Read in trees one at a time
         while((line = inB.readLine()) != null){
             if(templates == null) {
                 // Load first line into template
-                templates = new HashSet<>();
+                templates = new ArrayList<>();
                 templates.add(new Node(null, line));
                 continue;
             }
 
-            HashSet<Node> allCombined = new HashSet<>();
+            List<Node> allCombined = new ArrayList<>();
 
             Node input = new Node(null, line);
             for (Node template : templates) {
                 // Get all the ways we can combine input with template
-                HashSet<Node> combined = Node.reducedCombine(template, input);
+                List<Node> combined = Node.reducedCombine(template, input);
                 System.out.println("com: " + combined);
                 allCombined.addAll(combined);
             }
@@ -38,7 +39,7 @@ public class Main {
     public static void main(String[] args) {
         Main main = new Main();
         String inputFilename = "input.txt";
-        HashSet<Node> result;
+        List<Node> result;
         try {
             result = main.process(inputFilename);
             System.out.println("----------------------------------------");

+ 6 - 8
src/com/pact/Node.java

@@ -476,8 +476,8 @@ public class Node {
         }
     }
 
-    public static HashSet<Node> reducedCombine(Node node1, Node node2) {
-        HashSet<Node> result = new HashSet<>();
+    public static List<Node> reducedCombine(Node node1, Node node2) {
+        List<Node> result = new ArrayList<>();
 
         if (oneAreaShared(node1, node2)) {
             result.add(combineAtRoot(node1, node2));
@@ -499,16 +499,14 @@ public class Node {
         return removeDuplicates(result);
     }
 
-    private static HashSet<Node> removeDuplicates(HashSet<Node> h) {
-        Iterator it = h.iterator();
-        HashSet result = new HashSet();
-        while (it.hasNext()) {
-            Node curr = (Node)it.next();
+    private static List<Node> removeDuplicates(List<Node> h) {
+        HashSet<Node> result = new HashSet<>();
+        for (Node curr : h) {
             if (!curr.inSet(result)) {
                 result.add(curr);
             }
         }
-        return result;
+        return new ArrayList(result);
     }
 
     private static boolean oneAreaShared(Node node1, Node node2) {

+ 2 - 2
src/com/pact/test/NodeTest.java

@@ -50,7 +50,7 @@ class NodeTest {
     private void reducedCombineTest(String s1, String s2, String sResult){
         Node node1 = new Node(null, s1);
         Node node2 = new Node(null, s2);
-        HashSet<Node> results = Node.reducedCombine(node1, node2);
+        List<Node> results = Node.reducedCombine(node1, node2);
         for(Node result : results)
             System.out.println("" + node1 + " + " + node2 + " => " + result);
         assertTrue(results.size() == 1);
@@ -61,7 +61,7 @@ class NodeTest {
     private void reducedCombineTest(String s1, String s2, String[] sResults){
         Node node1 = new Node(null, s1);
         Node node2 = new Node(null, s2);
-        HashSet<Node> results = Node.reducedCombine(node1, node2);
+        List<Node> results = Node.reducedCombine(node1, node2);
         for(Node result : results)
             System.out.println("" + node1 + " + " + node2 + " => " + result);
         assertTrue(results.size() == sResults.length);