From 8be87e9412e70ee05a781999c926b690b4094e8d Mon Sep 17 00:00:00 2001 From: samskivert Date: Tue, 9 Feb 2010 20:45:39 +0000 Subject: [PATCH] More Comparable reduction. Made node final in NodeInfo. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2721 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/ShortestPath.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/java/com/samskivert/util/ShortestPath.java b/src/java/com/samskivert/util/ShortestPath.java index 6c0277b2..cfffc5d6 100644 --- a/src/java/com/samskivert/util/ShortestPath.java +++ b/src/java/com/samskivert/util/ShortestPath.java @@ -21,6 +21,7 @@ package com.samskivert.util; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -66,19 +67,18 @@ public class ShortestPath { HashMap> nodes = new HashMap>(); HashSet relaxed = new HashSet(); - ComparableArrayList> uptight = new ComparableArrayList>(); + SortableArrayList> uptight = new SortableArrayList>(); // initialize our searching info for (Iterator iter = graph.enumerateNodes(); iter.hasNext(); ) { - NodeInfo info = new NodeInfo(); - info.node = iter.next(); + NodeInfo info = new NodeInfo(iter.next()); if (info.node == start) { info.weightTo = 0; } uptight.add(info); nodes.put(info.node, info); } - uptight.sort(); + uptight.sort(WEIGHT_ORDER); // now execute the main part of the search while (uptight.size() > 0) { @@ -104,7 +104,7 @@ public class ShortestPath } } // now resort the uptight list - uptight.sort(); + uptight.sort(WEIGHT_ORDER); } // now trace the path from the final node back to the start @@ -118,10 +118,10 @@ public class ShortestPath } /** Used to maintain information during the shortest path search. */ - protected static final class NodeInfo implements Comparable> + protected static final class NodeInfo { /** The node for which we're representing information. */ - public T node; + public final T node; /** The cumulative weight to this node from the source. */ public int weightTo = Integer.MAX_VALUE / 4; @@ -129,9 +129,15 @@ public class ShortestPath /** The edge followed to reach this node along the shortest path. */ public V edgeTo; - /** We order ourselves by the cumulative weight to this node. */ - public int compareTo (NodeInfo o) { - return Comparators.compare(o.weightTo, weightTo); + public NodeInfo (T node) { + this.node = node; } } + + protected static final Comparator> WEIGHT_ORDER = + new Comparator>() { + public int compare (NodeInfo one, NodeInfo two) { + return Comparators.compare(two.weightTo, one.weightTo); + } + }; }