| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | * Created on Sep 24, 2005 | |
| 3 | * | |
| 4 | * Copyright (c) 2005, the JUNG Project and the Regents of the University | |
| 5 | * of California | |
| 6 | * All rights reserved. | |
| 7 | * | |
| 8 | * This software is open-source under the BSD license; see either | |
| 9 | * "license.txt" or | |
| 10 | * http://jung.sourceforge.net/license.txt for a description. | |
| 11 | */ | |
| 12 | package edu.uci.ics.jung.utils; | |
| 13 | ||
| 14 | import java.util.HashMap; | |
| 15 | import java.util.Iterator; | |
| 16 | import java.util.Map; | |
| 17 | import java.util.Set; | |
| 18 | ||
| 19 | import edu.uci.ics.jung.graph.Edge; | |
| 20 | import edu.uci.ics.jung.graph.Vertex; | |
| 21 | ||
| 22 | /** | |
| 23 | * A class which creates and maintains indices for parallel edges. | |
| 24 | * Parallel edges are defined here to be those edges of type <code>Edge</code> | |
| 25 | * that are returned by <code>v.findEdgeSet(w)</code> for some | |
| 26 | * <code>v</code> and <code>w</code>. | |
| 27 | * | |
| 28 | * <p>At this time, users are responsible for resetting the indices if changes to the | |
| 29 | * graph make it appropriate.</p> | |
| 30 | * | |
| 31 | * @author Joshua O'Madadhain | |
| 32 | * @author Tom Nelson | |
| 33 | * | |
| 34 | */ | |
| 35 | public class DefaultParallelEdgeIndexFunction implements ParallelEdgeIndexFunction | |
| 36 | { | |
| 37 | 0 | protected Map edge_index = new HashMap(); |
| 38 | ||
| 39 | 0 | private DefaultParallelEdgeIndexFunction() { } |
| 40 | ||
| 41 | public static DefaultParallelEdgeIndexFunction getInstance() { | |
| 42 | 0 | return new DefaultParallelEdgeIndexFunction(); |
| 43 | } | |
| 44 | /** | |
| 45 | * Returns the index for the specified edge. | |
| 46 | * Calculates the indices for <code>e</code> and for all edges parallel | |
| 47 | * to <code>e</code>. | |
| 48 | */ | |
| 49 | public int getIndex(Edge e) | |
| 50 | { | |
| 51 | 0 | Integer index = (Integer)edge_index.get(e); |
| 52 | 0 | if(index == null) |
| 53 | 0 | index = getIndex_internal(e); |
| 54 | 0 | return index.intValue(); |
| 55 | } | |
| 56 | ||
| 57 | protected Integer getIndex_internal(Edge e) | |
| 58 | { | |
| 59 | 0 | Pair endpoints = e.getEndpoints(); |
| 60 | 0 | Vertex u = (Vertex)(endpoints.getFirst()); |
| 61 | 0 | Vertex v = (Vertex)(endpoints.getSecond()); |
| 62 | 0 | Set commonEdgeSet = u.findEdgeSet(v); |
| 63 | 0 | int count = 0; |
| 64 | 0 | for(Iterator iterator=commonEdgeSet.iterator(); iterator.hasNext(); ) { |
| 65 | 0 | Edge other = (Edge)iterator.next(); |
| 66 | 0 | if (e.equals(other) == false) |
| 67 | { | |
| 68 | 0 | edge_index.put(other, new Integer(count)); |
| 69 | 0 | count++; |
| 70 | } | |
| 71 | } | |
| 72 | 0 | Integer index = new Integer(count); |
| 73 | 0 | edge_index.put(e, index); |
| 74 | ||
| 75 | 0 | return index; |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Resets the indices for this edge and its parallel edges. | |
| 80 | * Should be invoked when an edge parallel to <code>e</code> | |
| 81 | * has been added or removed. | |
| 82 | * @param e | |
| 83 | */ | |
| 84 | public void reset(Edge e) | |
| 85 | { | |
| 86 | 0 | getIndex_internal(e); |
| 87 | 0 | } |
| 88 | ||
| 89 | /** | |
| 90 | * Clears all edge indices for all edges in all graphs. | |
| 91 | * Does not recalculate the indices. | |
| 92 | */ | |
| 93 | public void reset() | |
| 94 | { | |
| 95 | 0 | edge_index.clear(); |
| 96 | 0 | } |
| 97 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |