示例#1
0
  /**
   * Given a set of SCC nodes make this the lead member of the SCC and reroute all incoming and
   * outgoing links accordingly. This eager rewrite is based on the assumption that there are few
   * cycles so it is better to rewrite once and keep the graph easy to traverse.
   */
  public void makeLeadNodeFor(Set<GraphNode> members) {
    // Accumulate all successors
    Set<GraphNode> newSucc = new HashSet<>();
    Set<GraphNode> newSuccClosed = new HashSet<>();
    for (GraphNode n : members) {
      newSucc.addAll(n.succ);
      newSuccClosed.addAll(n.succClosed);
    }
    newSucc.removeAll(members);
    newSuccClosed.removeAll(members);
    succ = newSucc;
    succClosed = newSuccClosed;

    // Rewrite all direct successors to have us as predecessor
    for (GraphNode n : succ) {
      n.pred.removeAll(members);
      n.pred.add(this);
    }

    // Find all predecessor nodes and relink link them to point to us
    Set<GraphNode> done = new HashSet<>();
    Set<GraphNode> newAliases = new HashSet<>();
    for (GraphNode member : members) {
      addSiblings(newAliases, member);
    }
    becomeLeaderOf(newAliases);
    for (GraphNode n : members) {
      if (n != this) {
        pred.addAll(n.pred);
        n.relocateAllRefTo(this, done);
        n.becomeSubordinateOf(this);
      }
    }
    pred.removeAll(members);
  }