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.Graph; | |
21 | import edu.uci.ics.jung.graph.Vertex; | |
22 | ||
23 | /** | |
24 | * A class which creates and maintains indices for parallel edges. | |
25 | * Parallel edges are defined here to be those edges of type <code>Edge</code> | |
26 | * that are returned by <code>v.findEdgeSet(w)</code> for some | |
27 | * <code>v</code> and <code>w</code>. | |
28 | * | |
29 | * <p>At this time, users are responsible for resetting the indices if changes to the | |
30 | * graph make it appropriate.</p> | |
31 | * | |
32 | * @author Joshua O'Madadhain | |
33 | */ | |
34 | public class ParallelEdgeIndexSingleton implements ParallelEdgeIndexFunction | |
35 | { | |
36 | ||
37 | private static ParallelEdgeIndexFunction instance; | |
38 | ||
39 | 0 | private ParallelEdgeIndexSingleton() {} |
40 | ||
41 | public static ParallelEdgeIndexFunction getInstance() { | |
42 | 0 | if(instance == null) { |
43 | 0 | instance = new ParallelEdgeIndexSingleton(); |
44 | } | |
45 | 0 | return instance; |
46 | } | |
47 | ||
48 | ||
49 | 0 | protected Map edge_index = new HashMap(); |
50 | ||
51 | /** | |
52 | * Returns the index for the specified edge. | |
53 | * Calculates the indices for <code>e</code> and for all edges parallel | |
54 | * to <code>e</code>. | |
55 | */ | |
56 | public int getIndex(Edge e) | |
57 | { | |
58 | 0 | Graph g = (Graph)e.getGraph(); |
59 | 0 | if (g == null) |
60 | 0 | throw new IllegalArgumentException("Orphaned edges may not be indexed."); |
61 | 0 | Integer index = (Integer)edge_index.get(new Pair(e, g)); |
62 | 0 | if(index == null) |
63 | 0 | index = getIndex_internal(e, g); |
64 | 0 | return index.intValue(); |
65 | } | |
66 | ||
67 | protected Integer getIndex_internal(Edge e, Graph g) | |
68 | { | |
69 | 0 | Pair endpoints = e.getEndpoints(); |
70 | 0 | Vertex u = (Vertex)(endpoints.getFirst()); |
71 | 0 | Vertex v = (Vertex)(endpoints.getSecond()); |
72 | 0 | Set commonEdgeSet = u.findEdgeSet(v); |
73 | 0 | int count = 0; |
74 | 0 | for(Iterator iterator=commonEdgeSet.iterator(); iterator.hasNext(); ) { |
75 | 0 | Edge other = (Edge)iterator.next(); |
76 | 0 | if (e.equals(other) == false) |
77 | { | |
78 | 0 | edge_index.put(new Pair(other, g), new Integer(count)); |
79 | 0 | count++; |
80 | } | |
81 | } | |
82 | 0 | Integer index = new Integer(count); |
83 | 0 | edge_index.put(e, index); |
84 | ||
85 | 0 | return index; |
86 | } | |
87 | ||
88 | /** | |
89 | * Resets the indices for this edge and its parallel edges. | |
90 | * Should be invoked when an edge parallel to <code>e</code> | |
91 | * has been added or removed. | |
92 | * @param e | |
93 | */ | |
94 | public void reset(Edge e) | |
95 | { | |
96 | 0 | Graph g = (Graph)e.getGraph(); |
97 | 0 | if (g == null) |
98 | 0 | throw new IllegalArgumentException("Orphaned edges may not be indexed."); |
99 | 0 | getIndex_internal(e, g); |
100 | 0 | } |
101 | ||
102 | /** | |
103 | * Clears all edge indices for all edges in all graphs. | |
104 | * Does not recalculate the indices. | |
105 | */ | |
106 | public void reset() | |
107 | { | |
108 | 0 | edge_index.clear(); |
109 | 0 | } |
110 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |