1 25 39 package org.jgrapht.alg; 40 41 import java.util.*; 42 43 import org.jgrapht.*; 44 import org.jgrapht.event.*; 45 import org.jgrapht.util.*; 46 47 48 66 public class NeighborIndex<V, E> 67 implements GraphListener<V, E> 68 { 69 70 72 Map<V, Neighbors<V, E>> neighborMap = new HashMap<V, Neighbors<V, E>>(); 73 private Graph<V, E> graph; 74 75 77 82 public NeighborIndex(Graph<V, E> g) 83 { 84 graph = g; 86 } 87 88 90 99 public Set<V> neighborsOf(V v) 100 { 101 return getNeighbors(v).getNeighbors(); 102 } 103 104 116 public List<V> neighborListOf(V v) 117 { 118 return getNeighbors(v).getNeighborList(); 119 } 120 121 124 public void edgeAdded(GraphEdgeChangeEvent<V, E> e) 125 { 126 E edge = e.getEdge(); 127 V source = graph.getEdgeSource(edge); 128 V target = graph.getEdgeTarget(edge); 129 getNeighbors(source).addNeighbor(target); 130 getNeighbors(target).addNeighbor(source); 131 } 132 133 136 public void edgeRemoved(GraphEdgeChangeEvent<V, E> e) 137 { 138 E edge = e.getEdge(); 139 V source = graph.getEdgeSource(edge); 140 V target = graph.getEdgeTarget(edge); 141 if (neighborMap.containsKey(source)) { 142 neighborMap.get(source).removeNeighbor(target); 143 } 144 if (neighborMap.containsKey(target)) { 145 neighborMap.get(target).removeNeighbor(source); 146 } 147 } 148 149 152 public void vertexAdded(GraphVertexChangeEvent<V> e) 153 { 154 } 156 157 160 public void vertexRemoved(GraphVertexChangeEvent<V> e) 161 { 162 neighborMap.remove(e.getVertex()); 163 } 164 165 private Neighbors<V, E> getNeighbors(V v) 166 { 167 Neighbors<V, E> neighbors = neighborMap.get(v); 168 if (neighbors == null) { 169 neighbors = new Neighbors<V, E>(v, 170 Graphs.neighborListOf(graph, v)); 171 neighborMap.put(v, neighbors); 172 } 173 return neighbors; 174 } 175 176 178 182 static class Neighbors<V, E> 183 { 184 private Map<V, ModifiableInteger> neighborCounts = 185 new LinkedHashMap<V, ModifiableInteger>(); 186 187 private Set<V> neighborSet = 190 Collections.unmodifiableSet( 191 neighborCounts.keySet()); 192 193 public Neighbors(V v, Collection<V> neighbors) 194 { 195 for (V neighbor : neighbors) { 197 addNeighbor(neighbor); 198 } 199 } 200 201 public void addNeighbor(V v) 202 { 203 ModifiableInteger count = neighborCounts.get(v); 204 if (count == null) { 205 count = new ModifiableInteger(1); 206 neighborCounts.put(v, count); 207 } else { 208 count.increment(); 209 } 210 } 211 212 public void removeNeighbor(V v) 213 { 214 ModifiableInteger count = neighborCounts.get(v); 215 if (count == null) { 216 throw new IllegalArgumentException ( 217 "Attempting to remove a neighbor that wasn't present"); 218 } 219 220 count.decrement(); 221 if (count.getValue() == 0) { 222 neighborCounts.remove(v); 223 } 224 } 225 226 public Set<V> getNeighbors() 227 { 228 return neighborSet; 229 } 230 231 public List<V> getNeighborList() 232 { 233 List<V> neighbors = new ArrayList<V>(); 234 for ( 235 Map.Entry<V, ModifiableInteger> entry 236 : neighborCounts.entrySet()) { 237 V v = entry.getKey(); 238 int count = entry.getValue().intValue(); 239 for (int i = 0; i < count; i++) { 240 neighbors.add(v); 241 } 242 } 243 return neighbors; 244 } 245 } 246 } 247 | Popular Tags |