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

soot.toolkits.graph.HashReversibleGraph Maven / Gradle / Ivy

package soot.toolkits.graph;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 2005 Navindra Umanee 
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Iterator;
import java.util.List;

/**
 * A reversible version of HashMutableDirectedGraph
 *
 * @author Navindra Umanee
 **/
public class HashReversibleGraph extends HashMutableDirectedGraph implements ReversibleGraph {
  protected boolean reversed;

  public HashReversibleGraph(DirectedGraph dg) {
    this();

    for (Iterator i = dg.iterator(); i.hasNext();) {
      N s = i.next();
      addNode(s);
    }

    for (Iterator i = dg.iterator(); i.hasNext();) {
      N s = i.next();
      List succs = dg.getSuccsOf(s);
      for (Iterator succsIt = succs.iterator(); succsIt.hasNext();) {
        N t = succsIt.next();
        addEdge(s, t);
      }
    }

    /* use the same heads and tails as the original graph */

    heads.clear();
    heads.addAll(dg.getHeads());
    tails.clear();
    tails.addAll(dg.getTails());
  }

  public HashReversibleGraph() {
    super();
    reversed = false;
  }

  public boolean isReversed() {
    return reversed;
  }

  public ReversibleGraph reverse() {
    reversed = !reversed;
    return this;
  }

  public void addEdge(N from, N to) {
    if (reversed) {
      super.addEdge(to, from);
    } else {
      super.addEdge(from, to);
    }
  }

  public void removeEdge(N from, N to) {
    if (reversed) {
      super.removeEdge(to, from);
    } else {
      super.removeEdge(from, to);
    }
  }

  public boolean containsEdge(N from, N to) {
    return reversed ? super.containsEdge(to, from) : super.containsEdge(from, to);
  }

  public List getHeads() {
    return reversed ? super.getTails() : super.getHeads();
  }

  public List getTails() {
    return reversed ? super.getHeads() : super.getTails();
  }

  public List getPredsOf(N s) {
    return reversed ? super.getSuccsOf(s) : super.getPredsOf(s);
  }

  public List getSuccsOf(N s) {
    return reversed ? super.getPredsOf(s) : super.getSuccsOf(s);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy