Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.ir.util;
  
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Map;
  import java.util.Set;

Meant to be single-threaded. More work on whole impl if not.
 
 public class DirectedGraph<T> {
     private Map<T, Vertex<T>> vertices = new HashMap<T, Vertex<T>>();
     private Set<Edge<T>> edges = new HashSet<Edge<T>>();
     private ArrayList inOrderVerticeData = new ArrayList();
     int vertexIDCounter = 0;
     
     public Collection<Vertex<T>> vertices() {
         return .values();
     }
     
     public Collection<Edge<T>> edges() {
         return ;
     }
     
     public Iterable<Edge<T>> edgesOfType(Object type) {
         return new EdgeTypeIterable<T>(type);
     }
     
     public Collection<T> allData() {
         return .keySet();
     }
    
    

Returns:
data in the order it was added to this graph.
 
     public Collection<T> getInorderData() {
         return ;
     }
     
     public void addEdge(T source, T destinationObject type) {
         vertexFor(source).addEdgeTo(destinationtype);
     }
     
     public void removeEdge(Edge edge) {
         edge.getSource().removeEdgeTo(edge.getDestination());
     }
     
     public void removeEdge(T source, T destination) {
         for (Edge edgevertexFor(source).getOutgoingEdges()) {
             if (edge.getDestination().getData() == destination) {
                 vertexFor(source).removeEdgeTo(edge.getDestination());
                 return;
             }
         }
     }
     
     public Vertex<T> findVertexFor(T data) {
         return .get(data);
     }
     
     public Vertex<T> vertexFor(T data) {
         Vertex vertex = .get(data);
         
         if (vertex != nullreturn vertex;
         
         vertex = new Vertex(thisdata++);
         .add(data);
         
         .put(datavertex);
         
         return vertex;
     }
     
     public void removeVertexFor(T data) {
         Vertex vertex = vertexFor(data);
         .remove(data);
         .remove(data);
         vertex.removeAllEdges();
     }
    
    

Returns:
the number of vertices in the graph.
 
     public int size() {
         return allData().size();
     }
 
     @Override
     public String toString() {
         StringBuilder buf = new StringBuilder();
         
         ArrayList<Vertex<T>> verts = new ArrayList<Vertex<T>>(.values());
         Collections.sort(verts);
         for (Vertex<T> vertexverts) {
             buf.append(vertex);
         }
        
        return buf.toString();
    }
New to GrepCode? Check out our FAQ X