Author: simonetripodi
Date: Tue Jun 28 09:12:36 2011
New Revision: 1140491
URL: http://svn.apache.org/viewvc?rev=1140491&view=rev
Log:
Edges stored in an hashset, in undirected graphs edges were enlisted twice
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java?rev=1140491&r1=1140490&r2=1140491&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
(original)
+++
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
Tue Jun 28 09:12:36 2011
@@ -23,6 +23,7 @@ import static java.util.Collections.unmo
import static java.util.Collections.unmodifiableSet;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -44,6 +45,8 @@ public abstract class BaseGraph<V extend
private final Map<V, Set<V>> adjacencyList = new HashMap<V, Set<V>>();
+ private final Set<E> allEdges = new HashSet<E>();
+
private final Map<VertexPair<V>, E> indexedEdges = new
HashMap<VertexPair<V>, E>();
private final Map<E, VertexPair<V>> indexedVertices = new HashMap<E,
VertexPair<V>>();
@@ -69,7 +72,7 @@ public abstract class BaseGraph<V extend
*/
public final Iterable<E> getEdges()
{
- return unmodifiableCollection( indexedEdges.values() );
+ return unmodifiableCollection( allEdges );
}
/**
@@ -77,7 +80,7 @@ public abstract class BaseGraph<V extend
*/
public int getSize()
{
- return indexedEdges.size();
+ return allEdges.size();
}
/**
@@ -155,6 +158,14 @@ public abstract class BaseGraph<V extend
}
/**
+ * @return
+ */
+ protected Set<E> getAllEdges()
+ {
+ return allEdges;
+ }
+
+ /**
* @return the indexedEdges
*/
protected Map<VertexPair<V>, E> getIndexedEdges()
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java?rev=1140491&r1=1140490&r2=1140491&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
(original)
+++
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
Tue Jun 28 09:12:36 2011
@@ -99,13 +99,6 @@ public abstract class BaseMutableGraph<V
*/
public void addEdge( V head, E e, V tail )
{
- internalAddEdge( head, e, tail );
-
- decorateAddEdge( head, e, tail );
- }
-
- protected void internalAddEdge( V head, E e, V tail )
- {
if ( head == null )
{
throw new GraphException( "Null head Vertex not admitted" );
@@ -119,6 +112,10 @@ public abstract class BaseMutableGraph<V
throw new GraphException( "Null tail Vertex not admitted" );
}
+ if ( !getAllEdges().add( e ) )
+ {
+ throw new GraphException( "Edge '%s' already present in the
Graph", e );
+ }
if ( !getAdjacencyList().containsKey( head ) )
{
throw new GraphException( "Head Vertex '%s' not present in the
Graph", head );
@@ -128,6 +125,13 @@ public abstract class BaseMutableGraph<V
throw new GraphException( "Tail Vertex '%s' not present in the
Graph", tail );
}
+ internalAddEdge( head, e, tail );
+
+ decorateAddEdge( head, e, tail );
+ }
+
+ protected void internalAddEdge( V head, E e, V tail )
+ {
getAdjacencyList().get( head ).add( tail );
VertexPair<V> vertexPair = new VertexPair<V>( head, tail );