All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.vcs.log.graph.impl.visible.LinearFragmentGenerator Maven / Gradle / Ivy

/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.vcs.log.graph.impl.visible;

import com.intellij.openapi.util.Pair;
import com.intellij.util.Function;
import com.intellij.vcs.log.graph.api.LiteLinearGraph;
import com.intellij.vcs.log.graph.api.elements.GraphEdge;
import com.intellij.vcs.log.graph.api.elements.GraphElement;
import com.intellij.vcs.log.graph.api.elements.GraphNode;
import com.intellij.vcs.log.graph.utils.LinearGraphUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.intellij.vcs.log.graph.api.LiteLinearGraph.NodeFilter.DOWN;
import static com.intellij.vcs.log.graph.api.LiteLinearGraph.NodeFilter.UP;

public class LinearFragmentGenerator {
  private static final int SHORT_FRAGMENT_MAX_SIZE = 10;
  private static final int MAX_SEARCH_SIZE = 10;

  @NotNull private final LiteLinearGraph myLinearGraph;

  @NotNull private final Set myPinnedNodes;

  private final Function> upNodesFun = new Function>() {
    @Override
    public List fun(Integer integer) {
      return myLinearGraph.getNodes(integer, UP);
    }
  };

  private final Function> downNodesFun = new Function>() {
    @Override
    public List fun(Integer integer) {
      return myLinearGraph.getNodes(integer, DOWN);
    }
  };

  public LinearFragmentGenerator(@NotNull LiteLinearGraph linearGraph, @NotNull Set pinnedNodes) {
    myLinearGraph = linearGraph;
    myPinnedNodes = pinnedNodes;
  }

  @Nullable
  public GraphFragment getRelativeFragment(@NotNull GraphElement element) {
    int upNodeIndex;
    int downNodeIndex;
    if (element instanceof GraphNode) {
      upNodeIndex = ((GraphNode)element).getNodeIndex();
      downNodeIndex = upNodeIndex;
    }
    else {
      Pair graphEdge = LinearGraphUtils.asNormalEdge(((GraphEdge)element));
      if (graphEdge == null) return null;

      upNodeIndex = graphEdge.first;
      downNodeIndex = graphEdge.second;
    }

    for (int i = 0; i < MAX_SEARCH_SIZE; i++) {
      GraphFragment graphFragment = getDownFragment(upNodeIndex);
      if (graphFragment != null && graphFragment.downNodeIndex >= downNodeIndex) return graphFragment;

      List upNodes = myLinearGraph.getNodes(upNodeIndex, UP);
      if (upNodes.size() != 1) {
        break;
      }
      upNodeIndex = upNodes.get(0);
    }

    return null;
  }

  @Nullable
  public GraphFragment getDownFragment(int upperVisibleNodeIndex) {
    Pair fragment = getFragment(upperVisibleNodeIndex, downNodesFun, upNodesFun, myPinnedNodes);
    return fragment == null ? null : new GraphFragment(fragment.first, fragment.second);
  }

  @Nullable
  public GraphFragment getUpFragment(int lowerNodeIndex) {
    Pair fragment = getFragment(lowerNodeIndex, upNodesFun, downNodesFun, myPinnedNodes);
    return fragment == null ? null : new GraphFragment(fragment.second, fragment.first);
  }

  @Nullable
  public GraphFragment getLongDownFragment(int rowIndex) {
    return getLongFragment(getDownFragment(rowIndex), Integer.MAX_VALUE);
  }

  @Nullable
  public GraphFragment getLongFragment(@NotNull GraphElement element) {
    return getLongFragment(getRelativeFragment(element), Integer.MAX_VALUE);
  }

  // for hover
  @Nullable
  public GraphFragment getPartLongFragment(@NotNull GraphElement element) {
    return getLongFragment(getRelativeFragment(element), 500);
  }

  @Nullable
  private GraphFragment getLongFragment(@Nullable GraphFragment startFragment, int bound) {
    if (startFragment == null) return null;

    GraphFragment shortFragment;

    int maxDown = startFragment.downNodeIndex;
    while ((shortFragment = getDownFragment(maxDown)) != null && !myPinnedNodes.contains(maxDown)) {
      maxDown = shortFragment.downNodeIndex;
      if (maxDown - startFragment.downNodeIndex > bound) break;
    }

    int maxUp = startFragment.upNodeIndex;
    while ((shortFragment = getUpFragment(maxUp)) != null && !myPinnedNodes.contains(maxUp)) {
      maxUp = shortFragment.upNodeIndex;
      if (startFragment.upNodeIndex - maxUp > bound) break;
    }

    if (maxUp != startFragment.upNodeIndex || maxDown != startFragment.downNodeIndex) {
      return new GraphFragment(maxUp, maxDown);
    }
    else {
      // start fragment is Simple
      if (myLinearGraph.getNodes(startFragment.upNodeIndex, DOWN).size() != 1) return startFragment;
    }
    return null;
  }

  @Nullable
  private static Pair getFragment(int startNode,
                                                    Function> getNextNodes,
                                                    Function> getPrevNodes,
                                                    Set thisNodeCantBeInMiddle) {
    Set blackNodes = new HashSet();
    blackNodes.add(startNode);

    Set grayNodes = new HashSet();
    grayNodes.addAll(getNextNodes.fun(startNode));

    int endNode = -1;
    while (blackNodes.size() < SHORT_FRAGMENT_MAX_SIZE) {
      int nextBlackNode = -1;
      for (int grayNode : grayNodes) {
        if (blackNodes.containsAll(getPrevNodes.fun(grayNode))) {
          nextBlackNode = grayNode;
          break;
        }
      }

      if (nextBlackNode == -1) return null;

      if (grayNodes.size() == 1) {
        endNode = nextBlackNode;
        break;
      }

      List nextGrayNodes = getNextNodes.fun(nextBlackNode);
      if (nextGrayNodes.isEmpty() || thisNodeCantBeInMiddle.contains(nextBlackNode)) return null;

      blackNodes.add(nextBlackNode);
      grayNodes.remove(nextBlackNode);
      grayNodes.addAll(nextGrayNodes);
    }

    if (endNode != -1) {
      return Pair.create(startNode, endNode);
    }
    else {
      return null;
    }
  }

  public static class GraphFragment {
    public final int upNodeIndex;
    public final int downNodeIndex;

    public GraphFragment(int upNodeIndex, int downNodeIndex) {
      this.upNodeIndex = upNodeIndex;
      this.downNodeIndex = downNodeIndex;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy