Skip to content

Commit

Permalink
backpointer should be singular
Browse files Browse the repository at this point in the history
  • Loading branch information
alexholmes committed Mar 17, 2012
1 parent 1141d99 commit a3cd85b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/manning/hip/ch7/shortestpath/Map.java
Expand Up @@ -33,24 +33,24 @@ protected void map(Text key, Text value, Context context)
//
int neighborDistance = node.getDistance() + 1;

// create the backpointers, which will append our own
// create the backpointer, which will append our own
// node name to the list
//
String backpointers = node.constructBackpointers(key.toString());
String backpointer = node.constructBackpointer(key.toString());

// go through all the nodes and propagate the distance to them
//
for (int i = 0; i < node.getAdjacentNodeNames().length; i++) {

String neighbor = node.getAdjacentNodeNames()[i];

// output the neighbor with the propagated distance and backpointers
// output the neighbor with the propagated distance and backpointer
//
outKey.set(neighbor);

Node adjacentNode = new Node()
.setDistance(neighborDistance)
.setBackpointers(backpointers);
.setBackpointer(backpointer);

outValue.set(adjacentNode.toString());
System.out.println(
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/manning/hip/ch7/shortestpath/Node.java
Expand Up @@ -7,7 +7,7 @@

public class Node {
private int distance = INFINITE;
private String backpointers;
private String backpointer;
private String[] adjacentNodeNames;

public static int INFINITE = Integer.MAX_VALUE;
Expand All @@ -22,19 +22,19 @@ public Node setDistance(int distance) {
return this;
}

public String getBackpointers() {
return backpointers;
public String getBackpointer() {
return backpointer;
}

public Node setBackpointers(String backpointers) {
this.backpointers = backpointers;
public Node setBackpointer(String backpointer) {
this.backpointer = backpointer;
return this;
}

public String constructBackpointers(String name) {
public String constructBackpointer(String name) {
StringBuilder backpointers = new StringBuilder();
if (StringUtils.trimToNull(getBackpointers()) != null) {
backpointers.append(getBackpointers()).append(":");
if (StringUtils.trimToNull(getBackpointer()) != null) {
backpointers.append(getBackpointer()).append(":");
}
backpointers.append(name);
return backpointers.toString();
Expand Down Expand Up @@ -62,7 +62,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(distance)
.append(fieldSeparator)
.append(backpointers);
.append(backpointer);

if (getAdjacentNodeNames() != null) {
sb.append(fieldSeparator)
Expand All @@ -81,7 +81,7 @@ public static Node fromMR(String value) throws IOException {
}
Node node = new Node()
.setDistance(Integer.valueOf(parts[0]))
.setBackpointers(StringUtils.trimToNull(parts[1]));
.setBackpointer(StringUtils.trimToNull(parts[1]));
if (parts.length > 2) {
node.setAdjacentNodeNames(Arrays.copyOfRange(parts, 2,
parts.length));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/manning/hip/ch7/shortestpath/Reduce.java
Expand Up @@ -52,7 +52,7 @@ public void reduce(Text key, Iterable<Text> values,

if(shortestAdjacentNode != null) {
originalNode.setDistance(minDistance);
originalNode.setBackpointers(shortestAdjacentNode.getBackpointers());
originalNode.setBackpointer(shortestAdjacentNode.getBackpointer());
}

outValue.set(originalNode.toString());
Expand All @@ -67,7 +67,7 @@ public void reduce(Text key, Iterable<Text> values,
PathCounter.TARGET_NODE_DISTANCE_COMPUTED);
counter.increment(minDistance);
context.getCounter(PathCounter.PATH.toString(),
shortestAdjacentNode.getBackpointers()).increment(1);
shortestAdjacentNode.getBackpointer()).increment(1);
}
}
}

0 comments on commit a3cd85b

Please sign in to comment.