Class Subgraph

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12  
 13 package org.locationtech.jts.planargraph;
 14  
 15 import java.util.ArrayList;
 16 import java.util.HashSet;
 17 import java.util.Iterator;
 18 import java.util.List;
 19 import java.util.Set;
 20  
 21 /**
 22  * A subgraph of a {@link PlanarGraph}.
 23  * A subgraph may contain any subset of {@link Edge}s
 24  * from the parent graph.
 25  * It will also automatically contain all {@link DirectedEdge}s
 26  * and {@link Node}s associated with those edges.
 27  * No new objects are created when edges are added -
 28  * all associated components must already exist in the parent graph.
 29  */
 30 public class Subgraph
 31 {
 32   protected PlanarGraph parentGraph;
 33   protected Set edges = new HashSet();
 34   protected List dirEdges = new ArrayList();
 35   protected NodeMap nodeMap = new NodeMap();
 36  
 37   /**
 38    * Creates a new subgraph of the given {@link PlanarGraph}
 39    *
 40    * @param parentGraph the parent graph
 41    */
 42   public Subgraph(PlanarGraph parentGraph) {
 43     this.parentGraph = parentGraph;
 44   }
 45  
 46   /**
 47    * Gets the {@link PlanarGraph} which this subgraph
 48    * is part of.
 49    *
 50    * @return the parent PlanarGraph
 51    */
 52   public PlanarGraph getParent()
 53   {
 54     return parentGraph;
 55   }
 56   /**
 57    * Adds an {@link Edge} to the subgraph.
 58    * The associated {@link DirectedEdge}s and {@link Node}s
 59    * are also added.
 60    *
 61    * @param e the edge to add
 62    */
 63   public void add(Edge e)
 64   {
 65     if (edges.contains(e)) return;
 66  
 67     edges.add(e);
 68     dirEdges.add(e.getDirEdge(0));
 69     dirEdges.add(e.getDirEdge(1));
 70     nodeMap.add(e.getDirEdge(0).getFromNode());
 71     nodeMap.add(e.getDirEdge(1).getFromNode());
 72   }
 73  
 74   /**
 75    * Returns an {@link Iterator} over the {@link DirectedEdge}s in this graph,
 76    * in the order in which they were added.
 77    *
 78    * @return an iterator over the directed edges
 79    *
 80    * @see #add(Edge)
 81    */
 82   public Iterator dirEdgeIterator()  {    return dirEdges.iterator();  }
 83  
 84   /**
 85    * Returns an {@link Iterator} over the {@link Edge}s in this graph,
 86    * in the order in which they were added.
 87    *
 88    * @return an iterator over the edges
 89    *
 90    * @see #add(Edge)
 91    */
 92   public Iterator edgeIterator()  {    return edges.iterator();  }
 93  
 94   /**
 95    * Returns an {@link Iterator} over the {@link Node}s in this graph.
 96    * @return an iterator over the nodes
 97    */
 98   public Iterator nodeIterator()  {    return nodeMap.iterator();  }
 99  
100   /**
101    * Tests whether an {@link Edge} is contained in this subgraph
102    * @param e the edge to test
103    * @return <code>true</code> if the edge is contained in this subgraph
104    */
105   public boolean contains(Edge e) { return edges.contains(e); }
106  
107 }
108