Changeset 2407 in josm for trunk/src/org/openstreetmap/josm/data/osm/visitor
- Timestamp:
- 2009-11-08T13:38:44+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/CollectBackReferencesVisitor.java
r2381 r2407 4 4 import java.util.Collection; 5 5 import java.util.HashSet; 6 import java.util.HashMap;7 import java.util.Map;8 6 9 7 import org.openstreetmap.josm.data.osm.DataSet; … … 11 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 12 10 import org.openstreetmap.josm.data.osm.Relation; 13 import org.openstreetmap.josm.data.osm.RelationMember;14 11 import org.openstreetmap.josm.data.osm.Way; 15 12 … … 23 20 public class CollectBackReferencesVisitor extends AbstractVisitor { 24 21 25 private final DataSet ds;26 22 private final boolean indirectRefs; 27 23 28 24 private Collection<OsmPrimitive> data = new HashSet<OsmPrimitive>(); 29 private Map<OsmPrimitive, Collection<OsmPrimitive>> lookupTable = new HashMap<OsmPrimitive, Collection<OsmPrimitive>>();30 25 31 26 32 27 /** 33 * Construct a back reference counter. 34 * has time complexity O(n) - so it is appropriate not to call it in cycle 35 * @param ds The dataset to operate on. 28 * @param ds This parameter is ignored 36 29 */ 37 30 public CollectBackReferencesVisitor(DataSet ds) { 38 this(ds,true);31 this(true); 39 32 } 40 33 41 34 /** 42 * Construct a back reference counter. 43 * has time complexity O(n) - so it is appropriate not to call it in cycle 44 * @param ds The dataset to operate on. 35 * @param ds This parameter is ignored 45 36 * @param indirectRefs Make also indirect references? 46 37 */ 47 38 public CollectBackReferencesVisitor(DataSet ds, boolean indirectRefs) { 48 this.ds = ds; 49 this.indirectRefs = indirectRefs; 50 if(ds != null) 51 makeLookupTable(); 39 this.indirectRefs = indirectRefs; 52 40 } 53 54 private void makeLookupTable() { 55 for (Way w : ds.getWays()) { 56 for (Node n : w.getNodes()) { 57 if (!lookupTable.containsKey(n)) lookupTable.put(n, new HashSet<OsmPrimitive>()); 58 lookupTable.get(n).add(w); 59 } 60 } 61 for (Relation r : ds.getRelations()) { 62 for (RelationMember m : r.getMembers()) { 63 OsmPrimitive o = m.getMember(); 64 if (!lookupTable.containsKey(o)) lookupTable.put(o, new HashSet<OsmPrimitive>()); 65 lookupTable.get(o).add(r); 66 } 67 } 41 42 public CollectBackReferencesVisitor(boolean indirectRefs) { 43 this.indirectRefs = indirectRefs; 68 44 } 45 69 46 70 47 /** … … 72 49 */ 73 50 public Collection<OsmPrimitive> getData(){ 74 return data; 51 return data; 75 52 } 76 53 77 54 /** 78 * Initialize data before associated visit calls 55 * Initialize data before associated visit calls 79 56 */ 80 57 public void initialize(){ 81 data = new HashSet<OsmPrimitive>(); 58 data = new HashSet<OsmPrimitive>(); 82 59 } 83 60 84 61 public void visit(OsmPrimitive o) { 85 if(lookupTable.containsKey(o)){ 86 Collection<OsmPrimitive> c = lookupTable.get(o); 87 Collection<OsmPrimitive> oldData = new HashSet<OsmPrimitive>(data); 88 data.addAll(c); 89 if(indirectRefs) 90 for(OsmPrimitive oo : c) 91 if(!oldData.contains(oo)) 92 visit(oo); 93 } 62 Collection<OsmPrimitive> c = o.getReferrers(); 63 Collection<OsmPrimitive> oldData = new HashSet<OsmPrimitive>(data); 64 data.addAll(c); 65 if(indirectRefs) { 66 for(OsmPrimitive oo : c) 67 if(!oldData.contains(oo)) { 68 visit(oo); 69 } 70 } 71 94 72 } 95 73 96 74 public void visit(Node n) { 97 visit((OsmPrimitive)n); 75 visit((OsmPrimitive)n); 98 76 } 99 77 100 78 public void visit(Way w) { 101 visit((OsmPrimitive)w); 79 visit((OsmPrimitive)w); 102 80 } 103 81 104 82 public void visit(Relation r) { 105 visit((OsmPrimitive)r); 83 visit((OsmPrimitive)r); 106 84 } 107 85 }
Note:
See TracChangeset
for help on using the changeset viewer.