From 916558a8b2a65897a5b0fc41f39285444c7d54cd Mon Sep 17 00:00:00 2001 From: mat kelcey Date: Sun, 7 Aug 2011 20:52:35 -0700 Subject: [PATCH] include redirect processing in place --- .../src/DistanceToPhilosophy.java | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/distanceToPhilosophy/src/DistanceToPhilosophy.java b/distanceToPhilosophy/src/DistanceToPhilosophy.java index 7b36970..6df684c 100644 --- a/distanceToPhilosophy/src/DistanceToPhilosophy.java +++ b/distanceToPhilosophy/src/DistanceToPhilosophy.java @@ -12,19 +12,23 @@ public class DistanceToPhilosophy { - private final Map tokenToIdx = new HashMap(); - private final Map idxToToken = new HashMap(); - private int tokenIdx = 0; - private final String startNode; + private TokenDictionary tokenDictionary = new TokenDictionary(); + + private final String startNode, edgesFile, redirectFile; private Map> edges = new HashMap>(); - public static void main(String s[]) throws IOException { - new DistanceToPhilosophy(s[0]).go(); + public static void main(String args[]) throws IOException { + if (args.length != 3) { + throw new RuntimeException("DistanceToPhilosophy "); + } + new DistanceToPhilosophy(args).go(); } - public DistanceToPhilosophy(String startNode) { - this.startNode = startNode; + public DistanceToPhilosophy(String[] args) { + this.startNode = args[0]; + this.edgesFile = args[1]; + this.redirectFile = args[2]; } public void go() throws IOException { @@ -41,13 +45,14 @@ private void parseInputFile() throws FileNotFoundException, IOException { String[] cols = next.split("\t"); // note: we want the reverse graph - int toNode = tokenForIdx(cols[0]); - int fromNode = tokenForIdx(cols[1]); + int toNode = tokenDictionary.tokenForIdx(cols[0]); + int fromNode = tokenDictionary.tokenForIdx(cols[1]); if (edges.containsKey(fromNode)) { List outboundEdges = edges.get(fromNode); if (outboundEdges.contains(toNode)) { - System.err.println("already and edge from "+fromNode+" ("+idxToToken.get(fromNode)+") to "+toNode+" ("+idxToToken.get(toNode)+")"); + System.err.println("already and edge from "+fromNode+ + " ("+tokenDictionary.idxToToken.get(fromNode)+") to "+toNode+" ("+tokenDictionary.idxToToken.get(toNode)+")"); } else { outboundEdges.add(toNode); @@ -65,19 +70,19 @@ private void parseInputFile() throws FileNotFoundException, IOException { } - if (!tokenToIdx.containsKey(startNode)) { + if (!tokenDictionary.tokenToIdx.containsKey(startNode)) { throw new RuntimeException("after parsing stdin never saw starting node ["+startNode+"]"); } } private void breadthFirstWalk() { Queue boundary = new LinkedList(); - boundary.add(tokenToIdx.get(startNode)); + boundary.add(tokenDictionary.tokenToIdx.get(startNode)); int distance = 0; while(boundary.size() != 0) { Queue frontier = new LinkedList(); for(int edge : boundary) { - System.out.println(idxToToken.get(edge) + "\t" + distance); + System.out.println(tokenDictionary.idxToToken.get(edge) + "\t" + distance); if (edges.containsKey(edge)) { frontier.addAll(edges.get(edge)); } @@ -88,15 +93,5 @@ private void breadthFirstWalk() { } } - public int tokenForIdx(String token) { - // build token -> idx - if (tokenToIdx.containsKey(token)) { - return tokenToIdx.get(token); - } - else { - tokenToIdx.put(token, tokenIdx); - idxToToken.put(tokenIdx, token); - return tokenIdx++; - } - } } +