package uk.co.alynn.games.suchrobot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class NodeSet implements Iterable<PathNode> {
    private List<PathNode> nodes = new ArrayList<PathNode>();
    private Map<String, List<PathNode>> directConnections = new HashMap<String, List<PathNode>>();
    
    public NodeSet() {
        
    }
    
    public void addNode(String type, String name, Rational x, Rational y) {
        PathNode newNode = new PathNode(name, x, y);
        nodes.add(newNode);
        directConnections.put(name, new ArrayList<PathNode>());
    }
    
    public void addConnection(String from, String to) {
        directConnections.get(from).add(lookup(to));
        directConnections.get(to).add(lookup(from));
    }
    
    public void compile() {
        System.err.println("compiled node set");
        for (PathNode node : this) {
            System.err.println("- " + node);
        }
    }
    
    public PathNode lookup(String name) {
        for (PathNode node : this) {
            if (node.name.equals(name))
                return node;
        }
        throw new RuntimeException("Missing path node " + name);
    }
    
    public Iterable<PathNode> connectionsFrom(PathNode node) {
        return directConnections.get(node.name);
    }
    
    public PathNode nextNodeFor(PathNode from, PathNode to) {
        return to; // TEMP: do some pathing
    }

    @Override
    public Iterator<PathNode> iterator() {
        return nodes.iterator();
    }
}

@@ -211,16 +211,759 @@
 Node%3E %7B%0A
+    private static final class RoutingKey %7B%0A        public final String sourceNode;%0A        public final String destNode;%0A        %0A        public RoutingKey(String src, String dst) %7B%0A            sourceNode = src;%0A            destNode = dst;%0A        %7D%0A        %0A        @Override%0A        public boolean equals(Object obj) %7B%0A            if (obj == this)%0A                return true;%0A            if (!(obj instanceof RoutingKey))%0A                return false;%0A            RoutingKey o = (RoutingKey)obj;%0A            return o.sourceNode.equals(sourceNode) && o.destNode.equals(destNode);%0A        %7D%0A        %0A        @Override%0A        public int hashCode() %7B%0A            return (903*sourceNode.hashCode()) + destNode.hashCode();%0A        %7D%0A    %7D%0A    %0A
     priv
@@ -1111,24 +1111,79 @@
 thNode%3E%3E();%0A
+    private Map%3CRoutingKey, PathNode%3E nextHops = null;%0A
     %0A    pub
@@ -1687,22 +1687,85 @@
 comp
-iled node set%22
+uting routing tables%22);%0A        nextHops = new HashMap%3CRoutingKey, PathNode%3E(
 );%0A 
@@ -1845,33 +1845,154 @@
  node);%0A        
-%7D
+    runDijkstra(node);%0A        %7D%0A    %7D%0A    %0A    private void runDijkstra(PathNode node) %7B%0A        // dijkstra's algorithm!
 %0A    %7D%0A    %0A    

Add some bees