1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.eclipse.jgit.revplot;
47
48 import java.text.MessageFormat;
49 import java.util.BitSet;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.HashSet;
53 import java.util.TreeSet;
54
55 import org.eclipse.jgit.internal.JGitText;
56 import org.eclipse.jgit.revwalk.RevCommitList;
57 import org.eclipse.jgit.revwalk.RevWalk;
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class PlotCommitList<L extends PlotLane> extends
72 RevCommitList<PlotCommit<L>> {
73 static final int MAX_LENGTH = 25;
74
75 private int positionsAllocated;
76
77 private final TreeSet<Integer> freePositions = new TreeSet<>();
78
79 private final HashSet<PlotLane> activeLanes = new HashSet<>(32);
80
81
82 private final HashMap<PlotLane, Integer> laneLength = new HashMap<>(
83 32);
84
85
86 @Override
87 public void clear() {
88 super.clear();
89 positionsAllocated = 0;
90 freePositions.clear();
91 activeLanes.clear();
92 laneLength.clear();
93 }
94
95
96 @Override
97 public void source(RevWalk w) {
98 if (!(w instanceof PlotWalk))
99 throw new ClassCastException(MessageFormat.format(JGitText.get().classCastNotA, PlotWalk.class.getName()));
100 super.source(w);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 public void findPassingThrough(final PlotCommit<L> currCommit,
122 final Collection<L> result) {
123 for (PlotLane p : currCommit.passingLanes)
124 result.add((L) p);
125 }
126
127
128 @Override
129 protected void enter(int index, PlotCommit<L> currCommit) {
130 setupChildren(currCommit);
131
132 final int nChildren = currCommit.getChildCount();
133 if (nChildren == 0) {
134 currCommit.lane = nextFreeLane();
135 } else if (nChildren == 1
136 && currCommit.children[0].getParentCount() < 2) {
137
138
139
140 @SuppressWarnings("unchecked")
141 final PlotCommit<L> c = currCommit.children[0];
142 currCommit.lane = c.lane;
143 Integer len = laneLength.get(currCommit.lane);
144 len = Integer.valueOf(len.intValue() + 1);
145 laneLength.put(currCommit.lane, len);
146 } else {
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 PlotLane reservedLane = null;
162 PlotCommit childOnReservedLane = null;
163 int lengthOfReservedLane = -1;
164
165 for (int i = 0; i < nChildren; i++) {
166 @SuppressWarnings("unchecked")
167 final PlotCommit<L> c = currCommit.children[i];
168 if (c.getParent(0) == currCommit) {
169 Integer len = laneLength.get(c.lane);
170
171
172 if (len.intValue() > lengthOfReservedLane) {
173 reservedLane = c.lane;
174 childOnReservedLane = c;
175 lengthOfReservedLane = len.intValue();
176 }
177 }
178 }
179
180 if (reservedLane != null) {
181 currCommit.lane = reservedLane;
182 laneLength.put(reservedLane,
183 Integer.valueOf(lengthOfReservedLane + 1));
184 handleBlockedLanes(index, currCommit, childOnReservedLane);
185 } else {
186 currCommit.lane = nextFreeLane();
187 handleBlockedLanes(index, currCommit, null);
188 }
189
190
191
192 for (int i = 0; i < nChildren; i++) {
193 final PlotCommit c = currCommit.children[i];
194 PlotCommit firstParent = (PlotCommit) c.getParent(0);
195 if (firstParent.lane != null && firstParent.lane != c.lane)
196 closeLane(c.lane);
197 }
198 }
199
200 continueActiveLanes(currCommit);
201 if (currCommit.getParentCount() == 0)
202 closeLane(currCommit.lane);
203 }
204
205 private void continueActiveLanes(PlotCommit currCommit) {
206 for (PlotLane lane : activeLanes)
207 if (lane != currCommit.lane)
208 currCommit.addPassingLane(lane);
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223 private void handleBlockedLanes(final int index, final PlotCommit currCommit,
224 final PlotCommit childOnLane) {
225 for (PlotCommit child : currCommit.children) {
226 if (child == childOnLane)
227 continue;
228
229
230
231 boolean childIsMerge = child.getParent(0) != currCommit;
232 if (childIsMerge) {
233 PlotLane laneToUse = currCommit.lane;
234 laneToUse = handleMerge(index, currCommit, childOnLane, child,
235 laneToUse);
236 child.addMergingLane(laneToUse);
237 } else {
238
239
240
241
242 PlotLane laneToUse = child.lane;
243 currCommit.addForkingOffLane(laneToUse);
244 }
245 }
246 }
247
248
249 private PlotLane handleMerge(final int index, final PlotCommit currCommit,
250 final PlotCommit childOnLane, PlotCommit child, PlotLane laneToUse) {
251
252
253
254 int childIndex = index;
255
256 BitSet blockedPositions = new BitSet();
257 for (int r = index - 1; r >= 0; r--) {
258 final PlotCommit rObj = get(r);
259 if (rObj == child) {
260 childIndex = r;
261 break;
262 }
263 addBlockedPosition(blockedPositions, rObj);
264 }
265
266
267
268 if (blockedPositions.get(laneToUse.getPosition())) {
269
270
271
272
273
274 boolean needDetour = false;
275 if (childOnLane != null) {
276 for (int r = index - 1; r > childIndex; r--) {
277 final PlotCommit rObj = get(r);
278 if (rObj == childOnLane) {
279 needDetour = true;
280 break;
281 }
282 }
283 }
284
285 if (needDetour) {
286
287
288
289
290
291 laneToUse = nextFreeLane(blockedPositions);
292 currCommit.addForkingOffLane(laneToUse);
293 closeLane(laneToUse);
294 } else {
295
296
297
298
299
300
301
302 int newPos = getFreePosition(blockedPositions);
303 freePositions.add(Integer.valueOf(laneToUse
304 .getPosition()));
305 laneToUse.position = newPos;
306 }
307 }
308
309
310 drawLaneToChild(index, child, laneToUse);
311 return laneToUse;
312 }
313
314
315
316
317
318
319
320
321
322 private void drawLaneToChild(final int commitIndex, PlotCommit child,
323 PlotLane laneToContinue) {
324 for (int r = commitIndex - 1; r >= 0; r--) {
325 final PlotCommit rObj = get(r);
326 if (rObj == child)
327 break;
328 if (rObj != null)
329 rObj.addPassingLane(laneToContinue);
330 }
331 }
332
333 private static void addBlockedPosition(BitSet blockedPositions,
334 final PlotCommit rObj) {
335 if (rObj != null) {
336 PlotLane lane = rObj.getLane();
337
338 if (lane != null)
339 blockedPositions.set(lane.getPosition());
340
341
342
343 for (PlotLane l : rObj.forkingOffLanes)
344 blockedPositions.set(l.getPosition());
345 for (PlotLane l : rObj.mergingLanes)
346 blockedPositions.set(l.getPosition());
347 }
348 }
349
350 @SuppressWarnings("unchecked")
351 private void closeLane(PlotLane lane) {
352 if (activeLanes.remove(lane)) {
353 recycleLane((L) lane);
354 laneLength.remove(lane);
355 freePositions.add(Integer.valueOf(lane.getPosition()));
356 }
357 }
358
359 private void setupChildren(PlotCommit<L> currCommit) {
360 final int nParents = currCommit.getParentCount();
361 for (int i = 0; i < nParents; i++)
362 ((PlotCommit) currCommit.getParent(i)).addChild(currCommit);
363 }
364
365 private PlotLane nextFreeLane() {
366 return nextFreeLane(null);
367 }
368
369 private PlotLane nextFreeLane(BitSet blockedPositions) {
370 final PlotLane p = createLane();
371 p.position = getFreePosition(blockedPositions);
372 activeLanes.add(p);
373 laneLength.put(p, Integer.valueOf(1));
374 return p;
375 }
376
377
378
379
380
381
382 private int getFreePosition(BitSet blockedPositions) {
383 if (freePositions.isEmpty())
384 return positionsAllocated++;
385
386 if (blockedPositions != null) {
387 for (Integer pos : freePositions)
388 if (!blockedPositions.get(pos.intValue())) {
389 freePositions.remove(pos);
390 return pos.intValue();
391 }
392 return positionsAllocated++;
393 } else {
394 final Integer min = freePositions.first();
395 freePositions.remove(min);
396 return min.intValue();
397 }
398 }
399
400
401
402
403
404
405
406
407 @SuppressWarnings("unchecked")
408 protected L createLane() {
409 return (L) new PlotLane();
410 }
411
412
413
414
415
416
417
418
419 protected void recycleLane(L lane) {
420
421 }
422 }