~ubuntu-branches/ubuntu/wily/aspectj/wily-proposed

« back to all changes in this revision

Viewing changes to org.aspectj/modules/util/src/org/aspectj/util/PartialOrder.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-15 23:54:31 UTC
  • mfrom: (1.2.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110315235431-iq2gxbsx08kpwuiw
* New upstream release.
* Updated Standards-Version to 3.9.1 (no changes needed).
* Fix local Javadoc links:
  - d/patches/07_javadoc_links.diff: Use locally installed
   javadoc packages and hyperlink with them.
  - d/control: Add B-D on default-java-doc and libasm3-java-doc.
* d/control: Drop B-D on itself (our new bootstrap infrastructure doesn't need
  that anymore).
* Split packages into :
  - aspectj: only contains CLI tools.
  - libaspectj-java: JAR librairies for /usr/share/java.
  - libaspectj-java-doc: 4 API's Javadoc.
  - aspectj-doc: Programming Guides and SDK Documentation.

Show diffs side-by-side

added added

removed removed

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