Changeset 3154 in josm


Ignore:
Timestamp:
Mar 23, 2010 9:12:35 PM (3 years ago)
Author:
jttt
Message:

Use positional search in QuadBuckets.contains() and remove()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/QuadBuckets.java

    r3151 r3154  
    1111import org.openstreetmap.josm.data.coor.QuadTiling; 
    1212 
     13/** 
     14 * Note: bbox of primitives added to QuadBuckets has to stay the same. In case of coordinate change, primitive must 
     15 * be removed and readded. 
     16 * 
     17 */ 
    1318public class QuadBuckets<T extends OsmPrimitive> implements Collection<T> 
    1419{ 
     
    127132                // changes made by threads will show up in children array too late, leading to QBLevel with all children 
    128133                // set to null 
     134                if (content == null) 
     135                    return false; 
    129136                boolean ret = this.content.remove(o); 
    130137                if (this.content.size() == 0) { 
     
    194201            } 
    195202            if (!hasChildren()) { 
    196                 // All items stay at this level (get_index == 1). Create at least first child to keep the contract 
     203                // All items stay at this level (get_index == -1). Create at least first child to keep the contract 
    197204                // that at least one child always exists 
    198205                children[0] = new QBLevel(this, 0); 
     
    378385            return ret; 
    379386        } 
    380         boolean contains(T n) 
    381         { 
    382             QBLevel res = find_exact(n); 
    383             if (res == null) 
    384                 return false; 
    385             return true; 
    386         } 
    387         QBLevel find_exact(T n) 
    388         { 
    389             if (content != null && content.contains(n)) 
    390                 return this; 
    391             return find_exact_child(n); 
    392         } 
    393         private QBLevel find_exact_child(T n) 
    394         { 
    395             QBLevel ret = null; 
    396             if (children == null) 
    397                 return ret; 
    398             for (QBLevel l : children) { 
    399                 if (l == null) { 
    400                     continue; 
    401                 } 
    402                 ret = l.find_exact(n); 
    403                 if (ret != null) { 
    404                     break; 
    405                 } 
    406             } 
    407             return ret; 
    408         } 
    409387 
    410388        void doAdd(T o) { 
     
    615593    { 
    616594        boolean changed = false; 
    617         for (Object o : objects) { 
    618             changed = changed | this.add(convert(o)); 
     595        for (T o : objects) { 
     596            changed = changed | this.add(o); 
    619597        } 
    620598        return changed; 
     
    639617        return this.remove(convert(o)); 
    640618    } 
    641     private boolean remove_slow(T removeme) 
    642     { 
    643         boolean ret = false; 
    644         Iterator<T> i = this.iterator(); 
    645         while (i.hasNext()) { 
    646             T o = i.next(); 
    647             if (o != removeme) { 
    648                 continue; 
    649             } 
    650             i.remove(); 
    651             ret = true; 
    652             break; 
    653         } 
    654         if (debug) { 
    655             out("qb slow remove result: " + ret); 
    656         } 
    657         return ret; 
    658     } 
    659     public boolean remove(T o) 
    660     { 
    661         /* 
    662          * We first try a locational search 
    663          */ 
    664         QBLevel bucket = root.find_exact(o); 
    665         /* 
    666          * That may fail because the object was 
    667          * moved or changed in some way, so we 
    668          * resort to an iterative search: 
    669          */ 
    670         if (bucket == null) 
    671             return remove_slow(o); 
    672         boolean ret = bucket.remove_content(o); 
    673         if (debug) { 
    674             out("qb remove result: " + ret); 
    675         } 
     619    public boolean remove(T o) { 
    676620        search_cache = null; // Search cache might point to one of removed buckets 
    677         return ret; 
    678     } 
    679     public boolean contains(Object o) 
    680     { 
    681         QBLevel bucket = root.find_exact(convert(o)); 
    682         if (bucket == null) 
    683             return false; 
    684         return true; 
    685     } 
     621        QBLevel bucket = root.findBucket(o.getBBox()); 
     622        return bucket.remove_content(o); 
     623    } 
     624    public boolean contains(Object o) { 
     625        QBLevel bucket = root.findBucket(convert(o).getBBox()); 
     626        return bucket != null && bucket.content != null && bucket.content.contains(o); 
     627    } 
     628 
    686629    public ArrayList<T> toArrayList() 
    687630    { 
Note: See TracChangeset for help on using the changeset viewer.