com.salesforce.jgrapht.graph.specifics.FastLookupUndirectedSpecifics Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AptSpringProcessor Show documentation
Show all versions of AptSpringProcessor Show documentation
This project contains the apt processor that implements all the checks enumerated in @Verify. It is a self contained, and
shaded jar.
/*
* (C) Copyright 2015-2017, by Joris Kinable and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package com.salesforce.jgrapht.graph.specifics;
import java.util.*;
import com.salesforce.jgrapht.alg.util.*;
import com.salesforce.jgrapht.graph.*;
import com.salesforce.jgrapht.util.*;
/**
* Fast implementation of UndirectedSpecifics. This class uses additional data structures to improve
* the performance of methods which depend on edge retrievals, e.g. getEdge(V u, V v),
* containsEdge(V u, V v),addEdge(V u, V v). A disadvantage is an increase in memory consumption. If
* memory utilization is an issue, use a {@link DirectedSpecifics} instead.
*
* @param the graph vertex type
* @param the graph edge type
*
* @author Joris Kinable
*/
public class FastLookupUndirectedSpecifics
extends UndirectedSpecifics
{
private static final long serialVersionUID = 225772727571597846L;
/*
* Maps a pair of vertices to a set of edges {(u,v)}. In case of a multigraph, all edges
* which touch both u,v are included in the set
*/
protected Map, ArrayUnenforcedSet> touchingVerticesToEdgeMap;
/**
* Construct a new fast lookup undirected specifics.
*
* @param abstractBaseGraph the graph for which these specifics are for
*/
public FastLookupUndirectedSpecifics(AbstractBaseGraph abstractBaseGraph)
{
this(abstractBaseGraph, new LinkedHashMap<>(), new ArrayUnenforcedSetEdgeSetFactory<>());
}
/**
* Construct a new fast lookup undirected specifics.
*
* @param abstractBaseGraph the graph for which these specifics are for
* @param vertexMap map for the storage of vertex edge sets
*/
public FastLookupUndirectedSpecifics(
AbstractBaseGraph abstractBaseGraph, Map> vertexMap)
{
this(abstractBaseGraph, vertexMap, new ArrayUnenforcedSetEdgeSetFactory<>());
}
/**
* Construct a new fast lookup undirected specifics.
*
* @param abstractBaseGraph the graph for which these specifics are for
* @param vertexMap map for the storage of vertex edge sets
* @param edgeSetFactory factory for the creation of vertex edge sets
*/
public FastLookupUndirectedSpecifics(
AbstractBaseGraph abstractBaseGraph, Map> vertexMap,
EdgeSetFactory edgeSetFactory)
{
super(abstractBaseGraph, vertexMap, edgeSetFactory);
this.touchingVerticesToEdgeMap = new HashMap<>();
}
/**
* {@inheritDoc}
*/
@Override
public Set getAllEdges(V sourceVertex, V targetVertex)
{
if (abstractBaseGraph.containsVertex(sourceVertex)
&& abstractBaseGraph.containsVertex(targetVertex))
{
Set edges =
touchingVerticesToEdgeMap.get(new UnorderedPair<>(sourceVertex, targetVertex));
return edges == null ? Collections.emptySet() : new ArrayUnenforcedSet<>(edges);
} else {
return null;
}
}
/**
* {@inheritDoc}
*/
@Override
public E getEdge(V sourceVertex, V targetVertex)
{
List edges =
touchingVerticesToEdgeMap.get(new UnorderedPair<>(sourceVertex, targetVertex));
if (edges == null || edges.isEmpty())
return null;
else
return edges.get(0);
}
/**
* {@inheritDoc}
*/
@Override
public void addEdgeToTouchingVertices(E e)
{
V source = abstractBaseGraph.getEdgeSource(e);
V target = abstractBaseGraph.getEdgeTarget(e);
getEdgeContainer(source).addEdge(e);
// Add edge to touchingVerticesToEdgeMap for the UnorderedPair {u,v}
Pair vertexPair = new UnorderedPair<>(source, target);
if (!touchingVerticesToEdgeMap.containsKey(vertexPair)) {
ArrayUnenforcedSet edgeSet = new ArrayUnenforcedSet<>();
edgeSet.add(e);
touchingVerticesToEdgeMap.put(vertexPair, edgeSet);
} else
touchingVerticesToEdgeMap.get(vertexPair).add(e);
if (!source.equals(target)) { // If not a self loop
getEdgeContainer(target).addEdge(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void removeEdgeFromTouchingVertices(E e)
{
V source = abstractBaseGraph.getEdgeSource(e);
V target = abstractBaseGraph.getEdgeTarget(e);
getEdgeContainer(source).removeEdge(e);
if (!source.equals(target))
getEdgeContainer(target).removeEdge(e);
// Remove the edge from the touchingVerticesToEdgeMap. If there are no more remaining edges
// for a pair
// of touching vertices, remove the pair from the map.
Pair vertexPair = new UnorderedPair<>(source, target);
if (touchingVerticesToEdgeMap.containsKey(vertexPair)) {
ArrayUnenforcedSet edgeSet = touchingVerticesToEdgeMap.get(vertexPair);
edgeSet.remove(e);
if (edgeSet.isEmpty())
touchingVerticesToEdgeMap.remove(vertexPair);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy