From b6519413dd7ae6958d342aeb57fb78722669fe5f Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Mon, 26 Jan 2015 15:11:54 +0300 Subject: [PATCH] [vcs-log][linear-bek] fix various code issues --- .../linearBek/LinearBekGraphBuilder.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/linearBek/LinearBekGraphBuilder.java b/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/linearBek/LinearBekGraphBuilder.java index 24d9a1e0d9d2..79016e4d9894 100644 --- a/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/linearBek/LinearBekGraphBuilder.java +++ b/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/linearBek/LinearBekGraphBuilder.java @@ -17,6 +17,7 @@ package com.intellij.vcs.log.graph.linearBek; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.ContainerUtilRt; import com.intellij.vcs.log.graph.api.EdgeFilter; import com.intellij.vcs.log.graph.api.GraphLayout; import com.intellij.vcs.log.graph.api.LinearGraph; @@ -46,40 +47,41 @@ class LinearBekGraphBuilder { } public LinearBekGraph build() { + Map nextHeadLayoutIndexes = ContainerUtilRt.newHashMap(myHeads.size()); + for (int i = 0; i < myHeads.size(); i++) { + if (i == myHeads.size() - 1) { + nextHeadLayoutIndexes.put(myHeads.get(i), Integer.MAX_VALUE); + } + else { + nextHeadLayoutIndexes.put(myHeads.get(i), myGraphLayout.getLayoutIndex(myHeads.get(i + 1))); + } + } + for (int i = myWorkingGraph.myGraph.nodesCount() - 1; i >= 0; i--) { - if (LinearGraphUtils.getDownNodes(myWorkingGraph, i).size() != 2) continue; - collapse(i, myGraphLayout.getOneOfHeadNodeIndex(i)); + List downNodes = ContainerUtil.sorted(LinearGraphUtils.getDownNodes(myWorkingGraph, i)); + if (downNodes.size() != 2) continue; + + int head = myGraphLayout.getOneOfHeadNodeIndex(i); + if (collapse(downNodes.get(1), downNodes.get(0), i, myGraphLayout.getLayoutIndex(head), nextHeadLayoutIndexes.get(head))) { + myWorkingGraph.apply(); + } + else { + myWorkingGraph.clear(); + } } return myWorkingGraph.createLinearBekGraph(); } - public void collapse(int mergeCommit, int head) { - int headNumber = myHeads.indexOf(head); - int nextHeadIndex = headNumber == myHeads.size() - 1 - ? Integer.MAX_VALUE - : myGraphLayout - .getLayoutIndex(myHeads.get(headNumber + 1)); // TODO dont make it bad, take a bad code and make it better - int headIndex = myGraphLayout.getLayoutIndex(head); - - List downNodes = ContainerUtil.sorted(LinearGraphUtils.getDownNodes(myWorkingGraph, mergeCommit)); - if (collapse(downNodes.get(1), downNodes.get(0), mergeCommit, headIndex, nextHeadIndex)) { - myWorkingGraph.apply(); - } - else { - myWorkingGraph.clear(); - } - } - private boolean collapse(int firstChild, int secondChild, int parent, int headIndex, int nextHeadIndex) { int x = myGraphLayout.getLayoutIndex(firstChild); int y = myGraphLayout.getLayoutIndex(secondChild); int k = 1; - PriorityQueue queue = new PriorityQueue(MAX_BLOCK_SIZE/*todo?*/, new GraphEdgeComparator()); + PriorityQueue queue = new PriorityQueue(MAX_BLOCK_SIZE, new GraphEdgeComparator()); queue.addAll(myWorkingGraph.getAdjacentEdges(secondChild, EdgeFilter.NORMAL_DOWN)); - Set definitelyNotTails = ContainerUtil.newHashSet(MAX_BLOCK_SIZE/*todo?*/); - Set tails = ContainerUtil.newHashSet(MAX_BLOCK_SIZE/*todo?*/); + Set definitelyNotTails = ContainerUtil.newHashSet(); + Set tails = ContainerUtil.newHashSet(); boolean mergeWithOldCommit = false; while (!queue.isEmpty()) { @@ -88,16 +90,13 @@ class LinearBekGraphBuilder { if (next == null) return false; // well, what do you do Integer upNodeIndex = nextEdge.getUpNodeIndex(); + assert upNodeIndex != null; // can not happen if (next == firstChild) { // found first child tails.add(upNodeIndex); mergeWithOldCommit = true; } - else if (next < secondChild + k) { - // or we were here before - // so doing nothing? - } else if (next == secondChild + k) { // all is fine, continuing k++; @@ -124,6 +123,8 @@ class LinearBekGraphBuilder { myWorkingGraph.removeEdge(upNodeIndex, next); // questionable -- we remove edges to the very old commits only for tails // done in sake of expanding dotted edges // also, should check (I guess?) that the edge is not too long + // another thing is that if li < x next can still be reachable from the firstChild + // but we do not know that unless we walk down from the firstChild and check that } } } @@ -132,8 +133,7 @@ class LinearBekGraphBuilder { if (k >= MAX_BLOCK_SIZE) { return false; } - if (Math.abs(myTimestampGetter.getTimestamp(secondChild) - myTimestampGetter.getTimestamp(secondChild + k - 1)) > - MAX_DELTA_TIME) { + if (Math.abs(myTimestampGetter.getTimestamp(secondChild) - myTimestampGetter.getTimestamp(secondChild + k - 1)) > MAX_DELTA_TIME) { // there is a big question what we should really check here // maybe we should also ensure that we do not remove edges to very old commits too return false;