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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 interface PartialComparable {
  28. /**
  29. * @return <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. 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. int fallbackCompareTo(Object other);
  44. }
  45. private static class SortObject<T extends PartialComparable> {
  46. T object;
  47. List<SortObject<T>> smallerObjects = new LinkedList<>();
  48. List<SortObject<T>> biggerObjects = new LinkedList<>();
  49. public SortObject(T o) {
  50. object = o;
  51. }
  52. boolean hasNoSmallerObjects() {
  53. return smallerObjects.size() == 0;
  54. }
  55. boolean removeSmallerObject(SortObject<T> o) {
  56. smallerObjects.remove(o);
  57. return hasNoSmallerObjects();
  58. }
  59. void addDirectedLinks(SortObject<T> 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 <T extends PartialComparable> void addNewPartialComparable(List<SortObject<T>> graph, T o) {
  77. SortObject<T> so = new SortObject<>(o);
  78. for (SortObject<T> other : graph) {
  79. so.addDirectedLinks(other);
  80. }
  81. graph.add(so);
  82. }
  83. private static <T extends PartialComparable> void removeFromGraph(List<SortObject<T>> graph, SortObject<T> o) {
  84. for (Iterator<SortObject<T>> i = graph.iterator(); i.hasNext();) {
  85. SortObject<T> other = i.next();
  86. if (o == other) {
  87. i.remove();
  88. }
  89. // ??? could use this to build up a new queue of objects with no
  90. // ??? smaller ones
  91. other.removeSmallerObject(o);
  92. }
  93. }
  94. /**
  95. * @param objects must all implement PartialComparable
  96. *
  97. * @return the same members as objects, but sorted according to their partial order. returns null if the objects are cyclical
  98. *
  99. */
  100. public static <T extends PartialComparable> List<T> sort(List<T> objects) {
  101. // lists of size 0 or 1 don't need any sorting
  102. if (objects.size() < 2) {
  103. return objects;
  104. }
  105. // ??? we might want to optimize a few other cases of small size
  106. // ??? I don't like creating this data structure, but it does give good
  107. // ??? separation of concerns.
  108. List<SortObject<T>> sortList = new LinkedList<>();
  109. for (T object : objects) {
  110. addNewPartialComparable(sortList, object);
  111. }
  112. // System.out.println(sortList);
  113. // now we have built our directed graph
  114. // use a simple sort algorithm from here
  115. // can increase efficiency later
  116. // List ret = new ArrayList(objects.size());
  117. final int N = objects.size();
  118. for (int index = 0; index < N; index++) {
  119. // System.out.println(sortList);
  120. // System.out.println("-->" + ret);
  121. SortObject<T> leastWithNoSmallers = null;
  122. for (SortObject<T> so: sortList) {
  123. if (so.hasNoSmallerObjects()) {
  124. if (leastWithNoSmallers == null || so.object.fallbackCompareTo(leastWithNoSmallers.object) < 0) {
  125. leastWithNoSmallers = so;
  126. }
  127. }
  128. }
  129. if (leastWithNoSmallers == null) {
  130. return null;
  131. }
  132. removeFromGraph(sortList, leastWithNoSmallers);
  133. objects.set(index, leastWithNoSmallers.object);
  134. }
  135. return objects;
  136. }
  137. /***********************************************************************************
  138. * /* a minimal testing harness
  139. ***********************************************************************************/
  140. static class Token implements PartialComparable {
  141. private String s;
  142. Token(String s) {
  143. this.s = s;
  144. }
  145. public int compareTo(Object other) {
  146. Token t = (Token) other;
  147. int cmp = s.charAt(0) - t.s.charAt(0);
  148. if (cmp == 1) {
  149. return 1;
  150. }
  151. if (cmp == -1) {
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. public int fallbackCompareTo(Object other) {
  157. return -s.compareTo(((Token) other).s);
  158. }
  159. public String toString() {
  160. return s;
  161. }
  162. }
  163. public static void main(String[] args) {
  164. List<Token> l = new ArrayList<>();
  165. l.add(new Token("a1"));
  166. l.add(new Token("c2"));
  167. l.add(new Token("b3"));
  168. l.add(new Token("f4"));
  169. l.add(new Token("e5"));
  170. l.add(new Token("d6"));
  171. l.add(new Token("c7"));
  172. l.add(new Token("b8"));
  173. l.add(new Token("z"));
  174. l.add(new Token("x"));
  175. l.add(new Token("f9"));
  176. l.add(new Token("e10"));
  177. l.add(new Token("a11"));
  178. l.add(new Token("d12"));
  179. l.add(new Token("b13"));
  180. l.add(new Token("c14"));
  181. System.out.println(l);
  182. sort(l);
  183. System.out.println(l);
  184. }
  185. }