comparison org.eclipse.draw2d/src/org/eclipse/draw2d/graph/RankSorter.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2003, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.graph.RankSorter;
14
15 import java.lang.all;
16 import org.eclipse.dwtxhelper.Random;
17 import org.eclipse.draw2d.graph.Node;
18 import org.eclipse.draw2d.graph.Rank;
19 import org.eclipse.draw2d.graph.DirectedGraph;
20 import org.eclipse.draw2d.graph.EdgeList;
21 import org.eclipse.draw2d.graph.Edge;
22 import org.eclipse.draw2d.graph.GraphUtilities;
23
24 /**
25 * Sorts Ranks during the up and down sweeps of the MinCross visitor.
26 * @author Randy Hudson
27 * @since 2.1.2
28 */
29 class RankSorter {
30
31 Random flipflop;
32 Node node;
33 double rankSize, prevRankSize, nextRankSize;
34 int currentRow;
35 Rank rank;
36 double progress;
37 DirectedGraph g;
38
39 public this(){
40 flipflop = new Random(3);
41 }
42
43 protected void assignIncomingSortValues() {
44 rankSize = rank.total;
45 prevRankSize = g.ranks.getRank(currentRow - 1).total;
46 if (currentRow < g.ranks.size() - 1)
47 nextRankSize = g.ranks.getRank(currentRow + 1).total;
48 for (int n = 0; n < rank.count(); n++) {
49 node = rank.getNode(n);
50 sortValueIncoming();
51 }
52 }
53
54 protected void assignOutgoingSortValues() {
55 rankSize = rank.total;
56 prevRankSize = g.ranks.getRank(currentRow + 1).total;
57 if (currentRow > 1)
58 nextRankSize = g.ranks.getRank(currentRow - 1).total;
59
60 for (int n = 0; n < rank.count(); n++) {
61 node = rank.getNode(n);
62 sortValueOutgoing();
63 }
64 }
65
66 double evaluateNodeIncoming() {
67 bool change = false;
68 EdgeList incoming = node.incoming;
69 do {
70 change = false;
71 for (int i = 0; i < incoming.size() - 1; i++) {
72 if (incoming.getSourceIndex(i) > incoming.getSourceIndex(i + 1)) {
73 Edge e = incoming.getEdge(i);
74 incoming.set(i, incoming.get(i + 1));
75 incoming.set(i + 1, e);
76 change = true;
77 }
78 }
79 } while (change);
80
81 int n = incoming.size();
82 if (n is 0) {
83 return node.index * prevRankSize / rankSize;
84 }
85 if (n % 2 is 1)
86 return incoming.getSourceIndex(n / 2);
87
88 int l = incoming.getSourceIndex(n / 2 - 1);
89 int r = incoming.getSourceIndex(n / 2);
90 if (progress >= 0.8 && n > 2) {
91 int dl = l - incoming.getSourceIndex(0);
92 int dr = incoming.getSourceIndex(n - 1) - r;
93 if (dl < dr)
94 return l;
95 if (dl > dr)
96 return r;
97 }
98 if (progress > 0.25 && progress < 0.75) {
99 if (flipflop.nextBoolean())
100 return (l + l + r) / 3.0;
101 else
102 return (r + r + l) / 3.0;
103 }
104 return (l + r) / 2.0;
105 }
106
107 double evaluateNodeOutgoing() {
108 bool change = false;
109 EdgeList outgoing = node.outgoing;
110 do {
111 change = false;
112 for (int i = 0; i < outgoing.size() - 1; i++) {
113 if (outgoing.getTargetIndex(i) > outgoing.getTargetIndex(i + 1)) {
114 Edge e = outgoing.getEdge(i);
115 outgoing.set(i, outgoing.get(i + 1));
116 outgoing.set(i + 1, e);
117 change = true;
118 }
119 }
120 } while (change);
121
122 int n = outgoing.size();
123 if (n is 0)
124 return node.index * prevRankSize / rankSize;
125 if (n % 2 is 1)
126 return outgoing.getTargetIndex(n / 2);
127 int l = outgoing.getTargetIndex(n / 2 - 1);
128 int r = outgoing.getTargetIndex(n / 2);
129 if (progress >= 0.8 && n > 2) {
130 int dl = l - outgoing.getTargetIndex(0);
131 int dr = outgoing.getTargetIndex(n - 1) - r;
132 if (dl < dr)
133 return l;
134 if (dl > dr)
135 return r;
136 }
137 if (progress > 0.25 && progress < 0.75) {
138 if (flipflop.nextBoolean())
139 return (l + l + r) / 3.0;
140 else
141 return (r + r + l) / 3.0;
142 }
143 return (l + r) / 2.0;
144 }
145
146 public void sortRankIncoming(DirectedGraph g, Rank rank, int row, double progress) {
147 this.currentRow = row;
148 this.rank = rank;
149 this.progress = progress;
150 assignIncomingSortValues();
151 sort();
152 postSort();
153 }
154
155 public void init(DirectedGraph g) {
156 this.g = g;
157 for (int i = 0; i < g.ranks.size(); i++) {
158 rank = g.ranks.getRank(i);
159
160 //Sort the ranks based on their constraints. Constraints are preserved throughout.
161 Collections.sort(rank, new class() Comparator {
162 public int compare(Object left, Object right) {
163 return (cast(Node)left).rowOrder - (cast(Node)right).rowOrder;
164 }
165 });
166 postSort();
167 }
168 }
169
170 void optimize(DirectedGraph g) {
171 }
172
173 protected void postSort() {
174 rank.assignIndices();
175 }
176
177 void sort() {
178 bool change;
179 do {
180 change = false;
181 for (int i = 0; i < rank.size() - 1; i++)
182 change |= swap(i);
183 if (!change)
184 break;
185 change = false;
186 for (int i = rank.size() - 2; i >= 0; i--)
187 change |= swap(i);
188 } while (change);
189 }
190
191 bool swap(int i) {
192 Node left = rank.getNode(i);
193 Node right = rank.getNode(i + 1);
194 if (GraphUtilities.isConstrained(left, right))
195 return false;
196 if (left.sortValue <= right.sortValue)
197 return false;
198 rank.set(i, right);
199 rank.set(i + 1, left);
200 return true;
201 }
202
203 public void sortRankOutgoing(DirectedGraph g, Rank rank, int row, double progress) {
204 this.currentRow = row;
205 this.rank = rank;
206 this.progress = progress;
207 assignOutgoingSortValues();
208 sort();
209 postSort();
210 }
211
212 void sortValueIncoming() {
213 node.sortValue = evaluateNodeIncoming();
214 //$TODO restore this optimization
215 // if (progress is 0.0 && !(node instanceof VirtualNode))
216 // node.sortValue = -1;
217 double value = evaluateNodeOutgoing();
218 if (value < 0)
219 value = node.index * nextRankSize / rankSize;
220 node.sortValue += value * progress;
221 // if (progress < 0.7 && node.sortValue !is -1)
222 // node.sortValue += Math.random() * rankSize / (5 + 8 * progress);
223 }
224
225 void sortValueOutgoing() {
226 node.sortValue = evaluateNodeOutgoing();
227 //$TODO restore this optimization
228 // if (progress is 0.0 && !(node instanceof VirtualNode))
229 // node.sortValue = -1;
230 double value = evaluateNodeIncoming();
231 if (value < 0)
232 value = node.index * nextRankSize / rankSize;
233 node.sortValue += value * progress;
234 // if (progress < 0.7 && node.sortValue !is -1)
235 // node.sortValue += Math.random() * rankSize / (5 + 8 * progress);
236 }
237
238 }