Changeset 5674 in josm for trunk


Ignore:
Timestamp:
2013-01-26T21:50:46+01:00 (11 years ago)
Author:
jttt
Message:

Move IdHash to Storage class and rename to PrimitiveIdHash to make it easier for other classes to use Storage class

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/command/PurgeCommand.java

    r4918 r5674  
    1616
    1717import org.openstreetmap.josm.data.osm.DataSet;
    18 import org.openstreetmap.josm.data.osm.Hash;
    1918import org.openstreetmap.josm.data.osm.Node;
    2019import org.openstreetmap.josm.data.osm.NodeData;
     
    6564
    6665    protected void saveIncomplete(Collection<OsmPrimitive> makeIncomplete) {
    67         makeIncompleteData = new Storage<PrimitiveData>(new Hash<PrimitiveData,PrimitiveData>() {
    68             public int getHashCode(PrimitiveData k) {
    69                 return (int) k.getUniqueId();
    70             }
    71 
    72             public boolean equals(PrimitiveData k, PrimitiveData t) {
    73                 return k.getUniqueId() == t.getUniqueId() && k.getType() == t.getType();
    74             }
    75         });
    76         makeIncompleteData_byPrimId = makeIncompleteData.foreignKey(new Hash<PrimitiveId, PrimitiveData>() {
    77             public int getHashCode(PrimitiveId k) {
    78                 return (int) k.getUniqueId();
    79             }
    80 
    81             public boolean equals(PrimitiveId k, PrimitiveData t) {
    82                 return k.getUniqueId() == t.getUniqueId() && k.getType() == t.getType();
    83             }
    84         });
     66        makeIncompleteData = new Storage<PrimitiveData>(new Storage.PrimitiveIdHash());
     67        makeIncompleteData_byPrimId = makeIncompleteData.foreignKey(new Storage.PrimitiveIdHash());
    8568
    8669        for (OsmPrimitive osm : makeIncomplete) {
     
    191174            }
    192175
    193         /**
    194          * Rest are relations. Do topological sorting on a DAG where each
    195          * arrow points from child to parent. (Because it is faster to
    196          * loop over getReferrers() than getMembers().)
    197          */
    198         Set<Relation> inR = (Set) in;
    199         Set<Relation> childlessR = new HashSet<Relation>();
    200         List<Relation> outR = new ArrayList<Relation>(inR.size());
    201 
    202         HashMap<Relation, Integer> numChilds = new HashMap<Relation, Integer>();
    203 
    204         // calculate initial number of childs
    205         for (Relation r : inR) {
    206             numChilds.put(r, 0);
    207         }
    208         for (Relation r : inR) {
    209             for (OsmPrimitive parent : r.getReferrers()) {
    210                 if (!(parent instanceof Relation))
    211                     throw new AssertionError();
    212                 Integer i = numChilds.get(parent);
    213                 if (i != null) {
    214                     numChilds.put((Relation)parent, i+1);
    215                 }
    216             }
    217         }
    218         for (Relation r : inR) {
    219             if (numChilds.get(r).equals(0)) {
    220                 childlessR.add(r);
    221             }
    222         }
    223 
    224         while (!childlessR.isEmpty()) {
    225             // Identify one childless Relation and
    226             // let it virtually die. This makes other
    227             // relations childless.
    228             Iterator<Relation> it  = childlessR.iterator();
    229             Relation next = it.next();
    230             it.remove();
    231             outR.add(next);
    232 
    233             for (OsmPrimitive parentPrim : next.getReferrers()) {
    234                 Relation parent = (Relation) parentPrim;
    235                 Integer i = numChilds.get(parent);
    236                 if (i != null) {
    237                     numChilds.put(parent, i-1);
    238                     if (i-1 == 0) {
    239                         childlessR.add(parent);
    240                     }
    241                 }
    242             }
    243         }
    244 
    245         if (outR.size() != inR.size())
    246             throw new AssertionError("topo sort algorithm failed");
    247 
    248         out.addAll(outR);
    249 
    250         return out;
     176            /**
     177             * Rest are relations. Do topological sorting on a DAG where each
     178             * arrow points from child to parent. (Because it is faster to
     179             * loop over getReferrers() than getMembers().)
     180             */
     181            Set<Relation> inR = (Set) in;
     182            Set<Relation> childlessR = new HashSet<Relation>();
     183            List<Relation> outR = new ArrayList<Relation>(inR.size());
     184
     185            HashMap<Relation, Integer> numChilds = new HashMap<Relation, Integer>();
     186
     187            // calculate initial number of childs
     188            for (Relation r : inR) {
     189                numChilds.put(r, 0);
     190            }
     191            for (Relation r : inR) {
     192                for (OsmPrimitive parent : r.getReferrers()) {
     193                    if (!(parent instanceof Relation))
     194                        throw new AssertionError();
     195                    Integer i = numChilds.get(parent);
     196                    if (i != null) {
     197                        numChilds.put((Relation)parent, i+1);
     198                    }
     199                }
     200            }
     201            for (Relation r : inR) {
     202                if (numChilds.get(r).equals(0)) {
     203                    childlessR.add(r);
     204                }
     205            }
     206
     207            while (!childlessR.isEmpty()) {
     208                // Identify one childless Relation and
     209                // let it virtually die. This makes other
     210                // relations childless.
     211                Iterator<Relation> it  = childlessR.iterator();
     212                Relation next = it.next();
     213                it.remove();
     214                outR.add(next);
     215
     216                for (OsmPrimitive parentPrim : next.getReferrers()) {
     217                    Relation parent = (Relation) parentPrim;
     218                    Integer i = numChilds.get(parent);
     219                    if (i != null) {
     220                        numChilds.put(parent, i-1);
     221                        if (i-1 == 0) {
     222                            childlessR.add(parent);
     223                        }
     224                    }
     225                }
     226            }
     227
     228            if (outR.size() != inR.size())
     229                throw new AssertionError("topo sort algorithm failed");
     230
     231            out.addAll(outR);
     232
     233            return out;
    251234    }
    252235
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r5360 r5674  
    100100    private static final int MAX_EVENTS = 1000;
    101101
    102     private static class IdHash implements Hash<PrimitiveId,OsmPrimitive> {
    103 
    104         public int getHashCode(PrimitiveId k) {
    105             return (int)k.getUniqueId() ^ k.getType().hashCode();
    106         }
    107 
    108         public boolean equals(PrimitiveId key, OsmPrimitive value) {
    109             if (key == null || value == null) return false;
    110             return key.getUniqueId() == value.getUniqueId() && key.getType() == value.getType();
    111         }
    112     }
    113 
    114     private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash(), true);
    115     private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash());
     102    private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new Storage.PrimitiveIdHash(), true);
     103    private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new Storage.PrimitiveIdHash());
    116104    private CopyOnWriteArrayList<DataSetListener> listeners = new CopyOnWriteArrayList<DataSetListener>();
    117105
     
    126114
    127115    private int highlightUpdateCount;
    128    
     116
    129117    private boolean uploadDiscouraged = false;
    130118
     
    486474        return new SubclassFilteredCollection<OsmPrimitive, OsmPrimitive>(getAllSelected(), OsmPrimitive.nonDeletedPredicate);
    487475    }
    488    
     476
    489477    /**
    490478     * Replies an unmodifiable collection of primitives currently selected
     
    11771165     */
    11781166    public void mergeFrom(DataSet from) {
    1179         mergeFrom(from, null);
    1180     }
    1181    
     1167        mergeFrom(from, null);
     1168    }
     1169
    11821170    /**
    11831171     * Moves all primitives and datasources from DataSet "from" to this DataSet
  • trunk/src/org/openstreetmap/josm/data/osm/Storage.java

    r5170 r5674  
    8787 */
    8888public class Storage<T> extends AbstractSet<T> {
     89
     90    public static class PrimitiveIdHash implements Hash<PrimitiveId, PrimitiveId> {
     91
     92        public int getHashCode(PrimitiveId k) {
     93            return (int)k.getUniqueId() ^ k.getType().hashCode();
     94        }
     95
     96        public boolean equals(PrimitiveId key, PrimitiveId value) {
     97            if (key == null || value == null) return false;
     98            return key.getUniqueId() == value.getUniqueId() && key.getType() == value.getType();
     99        }
     100    }
     101
    89102    private final Hash<? super T,? super T> hash;
    90103    private T[] data;
Note: See TracChangeset for help on using the changeset viewer.