1 24 25 package org.aspectj.util; 26 27 import java.util.*; 28 29 30 34 35 public class PartialOrder { 36 37 40 public static interface PartialComparable { 41 51 public int compareTo(Object other); 52 53 58 public int fallbackCompareTo(Object other); 59 } 60 61 private static class SortObject { 62 PartialComparable object; 63 List smallerObjects = new LinkedList(); 64 List biggerObjects = new LinkedList(); 65 66 public SortObject(PartialComparable o) { 67 object = o; 68 } 69 70 boolean hasNoSmallerObjects() { return smallerObjects.size() == 0; } 71 72 boolean removeSmallerObject(SortObject o) { 73 smallerObjects.remove(o); 74 return hasNoSmallerObjects(); 75 } 76 77 void addDirectedLinks(SortObject other) { 78 int cmp = object.compareTo(other.object); 79 if (cmp == 0) return; 80 if (cmp > 0) { 81 this.smallerObjects.add(other); 82 other.biggerObjects.add(this); 83 } else { 84 this.biggerObjects.add(other); 85 other.smallerObjects.add(this); 86 } 87 } 88 89 public String toString() { 90 return object.toString(); } 92 } 93 94 private static void addNewPartialComparable(List graph, PartialComparable o) { 95 SortObject so = new SortObject(o); 96 for (Iterator i = graph.iterator(); i.hasNext(); ) { 97 SortObject other = (SortObject)i.next(); 98 so.addDirectedLinks(other); 99 } 100 graph.add(so); 101 } 102 103 private static void removeFromGraph(List graph, SortObject o) { 104 for (Iterator i = graph.iterator(); i.hasNext(); ) { 105 SortObject other = (SortObject)i.next(); 106 107 if (o == other) i.remove(); 108 other.removeSmallerObject(o); 111 } 112 } 113 114 121 public static List sort(List objects) { 122 if (objects.size() < 2) return objects; 124 125 127 List sortList = new LinkedList(); for (Iterator i = objects.iterator(); i.hasNext(); ) { 131 addNewPartialComparable(sortList, (PartialComparable)i.next()); 132 } 133 134 136 final int N = objects.size(); 141 for (int index = 0; index < N; index++) { 142 145 SortObject leastWithNoSmallers = null; 146 147 for (Iterator i = sortList.iterator(); i.hasNext(); ) { 148 SortObject so = (SortObject)i.next(); 149 if (so.hasNoSmallerObjects()) { 151 if (leastWithNoSmallers == null || 152 so.object.fallbackCompareTo(leastWithNoSmallers.object) < 0) 153 { 154 leastWithNoSmallers = so; 155 } 156 } 157 } 158 159 if (leastWithNoSmallers == null) return null; 160 161 removeFromGraph(sortList, leastWithNoSmallers); 162 objects.set(index, leastWithNoSmallers.object); 163 } 164 165 return objects; 166 } 167 168 171 static class Token implements PartialComparable { 172 private String s; 173 174 Token(String s) { 175 this.s = s; 176 } 177 178 public int compareTo(Object other) { 179 Token t = (Token)other; 180 181 int cmp = s.charAt(0) - t.s.charAt(0); 182 if (cmp == 1) return 1; 183 if (cmp == -1) return -1; 184 return 0; 185 } 186 187 public int fallbackCompareTo(Object other) { 188 return -s.compareTo( ((Token)other).s ); 189 } 190 191 public String toString() { 192 return s; 193 } 194 } 195 196 public static void main(String [] args) { 197 List l = new ArrayList(); 198 l.add(new Token("a1")); 199 l.add(new Token("c2")); 200 l.add(new Token("b3")); 201 l.add(new Token("f4")); 202 l.add(new Token("e5")); 203 l.add(new Token("d6")); 204 l.add(new Token("c7")); 205 l.add(new Token("b8")); 206 207 l.add(new Token("z")); 208 l.add(new Token("x")); 209 210 l.add(new Token("f9")); 211 l.add(new Token("e10")); 212 l.add(new Token("a11")); 213 l.add(new Token("d12")); 214 l.add(new Token("b13")); 215 l.add(new Token("c14")); 216 217 System.out.println(l); 218 219 sort(l); 220 221 System.out.println(l); 222 } 223 } 224 | Popular Tags |