You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PartialOrder.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.util;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. /**
  19. * This class implements a partial order
  20. *
  21. * It includes routines for doing a topo-sort
  22. */
  23. public class PartialOrder {
  24. /**
  25. * All classes that want to be part of a partial order must implement PartialOrder.PartialComparable.
  26. */
  27. public static interface PartialComparable {
  28. /**
  29. * @returns <ul>
  30. * <li>+1 if this is greater than other</li>
  31. * <li>-1 if this is less than other</li>
  32. * <li>0 if this is not comparable to other</li>
  33. * </ul>
  34. *
  35. * <b> Note: returning 0 from this method doesn't mean the same thing as returning 0 from
  36. * java.util.Comparable.compareTo()</b>
  37. */
  38. public int compareTo(Object other);
  39. /**
  40. * This method can provide a deterministic ordering for elements that are strictly not comparable. If you have no need for
  41. * this, this method can just return 0 whenever called.
  42. */
  43. public int fallbackCompareTo(Object other);
  44. }
  45. private static class SortObject {
  46. PartialComparable object;
  47. List<SortObject> smallerObjects = new LinkedList<SortObject>();
  48. List<SortObject> biggerObjects = new LinkedList<SortObject>();
  49. public SortObject(PartialComparable o) {
  50. object = o;
  51. }
  52. boolean hasNoSmallerObjects() {
  53. return smallerObjects.size() == 0;
  54. }
  55. boolean removeSmallerObject(SortObject o) {
  56. smallerObjects.remove(o);
  57. return hasNoSmallerObjects();
  58. }
  59. void addDirectedLinks(SortObject other) {
  60. int cmp = object.compareTo(other.object);
  61. if (cmp == 0) {
  62. return;
  63. }
  64. if (cmp > 0) {
  65. this.smallerObjects.add(other);
  66. other.biggerObjects.add(this);
  67. } else {
  68. this.biggerObjects.add(other);
  69. other.smallerObjects.add(this);
  70. }
  71. }
  72. public String toString() {
  73. return object.toString(); // +smallerObjects+biggerObjects;
  74. }
  75. }
  76. private static void addNewPartialComparable(List<SortObject> graph, PartialComparable o) {
  77. SortObject so = new SortObject(o);
  78. for (Iterator<SortObject> i = graph.iterator(); i.hasNext();) {
  79. SortObject other = i.next();
  80. so.addDirectedLinks(other);
  81. }
  82. graph.add(so);
  83. }
  84. private static void removeFromGraph(List<SortObject> graph, SortObject o) {
  85. for (Iterator<SortObject> i = graph.iterator(); i.hasNext();) {
  86. SortObject other = i.next();
  87. if (o == other) {
  88. i.remove();
  89. }
  90. // ??? could use this to build up a new queue of objects with no
  91. // ??? smaller ones
  92. other.removeSmallerObject(o);
  93. }
  94. }
  95. /**
  96. * @param objects must all implement PartialComparable
  97. *
  98. * @returns the same members as objects, but sorted according to their partial order. returns null if the objects are cyclical
  99. *
  100. */
  101. public static List sort(List objects) {
  102. // lists of size 0 or 1 don't need any sorting
  103. if (objects.size() < 2) {
  104. return objects;
  105. }
  106. // ??? we might want to optimize a few other cases of small size
  107. // ??? I don't like creating this data structure, but it does give good
  108. // ??? separation of concerns.
  109. List<SortObject> sortList = new LinkedList<SortObject>(); // objects.size());
  110. for (Iterator i = objects.iterator(); i.hasNext();) {
  111. addNewPartialComparable(sortList, (PartialComparable) i.next());
  112. }
  113. // System.out.println(sortList);
  114. // now we have built our directed graph
  115. // use a simple sort algorithm from here
  116. // can increase efficiency later
  117. // List ret = new ArrayList(objects.size());
  118. final int N = objects.size();
  119. for (int index = 0; index < N; index++) {
  120. // System.out.println(sortList);
  121. // System.out.println("-->" + ret);
  122. SortObject leastWithNoSmallers = null;
  123. for (Iterator i = sortList.iterator(); i.hasNext();) {
  124. SortObject so = (SortObject) i.next();
  125. // System.out.println(so);
  126. if (so.hasNoSmallerObjects()) {
  127. if (leastWithNoSmallers == null || so.object.fallbackCompareTo(leastWithNoSmallers.object) < 0) {
  128. leastWithNoSmallers = so;
  129. }
  130. }
  131. }
  132. if (leastWithNoSmallers == null) {
  133. return null;
  134. }
  135. removeFromGraph(sortList, leastWithNoSmallers);
  136. objects.set(index, leastWithNoSmallers.object);
  137. }
  138. return objects;
  139. }
  140. /***********************************************************************************
  141. * /* a minimal testing harness
  142. ***********************************************************************************/
  143. static class Token implements PartialComparable {
  144. private String s;
  145. Token(String s) {
  146. this.s = s;
  147. }
  148. public int compareTo(Object other) {
  149. Token t = (Token) other;
  150. int cmp = s.charAt(0) - t.s.charAt(0);
  151. if (cmp == 1) {
  152. return 1;
  153. }
  154. if (cmp == -1) {
  155. return -1;
  156. }
  157. return 0;
  158. }
  159. public int fallbackCompareTo(Object other) {
  160. return -s.compareTo(((Token) other).s);
  161. }
  162. public String toString() {
  163. return s;
  164. }
  165. }
  166. public static void main(String[] args) {
  167. List<Token> l = new ArrayList<Token>();
  168. l.add(new Token("a1"));
  169. l.add(new Token("c2"));
  170. l.add(new Token("b3"));
  171. l.add(new Token("f4"));
  172. l.add(new Token("e5"));
  173. l.add(new Token("d6"));
  174. l.add(new Token("c7"));
  175. l.add(new Token("b8"));
  176. l.add(new Token("z"));
  177. l.add(new Token("x"));
  178. l.add(new Token("f9"));
  179. l.add(new Token("e10"));
  180. l.add(new Token("a11"));
  181. l.add(new Token("d12"));
  182. l.add(new Token("b13"));
  183. l.add(new Token("c14"));
  184. System.out.println(l);
  185. sort(l);
  186. System.out.println(l);
  187. }
  188. }