Changeset 3157 in josm


Ignore:
Timestamp:
2010-03-25T21:20:58+01:00 (14 years ago)
Author:
jttt
Message:

Keep size in QuadBuckets instead of calculating it on demand

File:
1 edited

Legend:

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

    r3154 r3157  
    349349            return next.nextContentNode();
    350350        }
    351         int size()
    352         {
    353             if (isLeaf())
    354                 return size_leaf();
    355             return size_branch();
    356         }
    357         private int size_leaf()
    358         {
    359             if (content == null) {
    360                 if (debug) {
    361                     out("["+level+"] leaf size: null content, children? " + Arrays.toString(children));
    362                 }
    363                 return 0;
    364             }
    365             if (debug) {
    366                 out("["+level+"] leaf size: " + content.size());
    367             }
    368             return content.size();
    369         }
    370         private int size_branch()
    371         {
    372             int ret = 0;
    373             for (QBLevel l : children) {
    374                 if (l == null) {
    375                     continue;
    376                 }
    377                 ret += l.size();
    378             }
    379             if (content != null) {
    380                 ret += content.size();
    381             }
    382             if (debug) {
    383                 out("["+level+"] branch size: " + ret);
    384             }
    385             return ret;
    386         }
    387351
    388352        void doAdd(T o) {
     
    547511    private QBLevel root;
    548512    private QBLevel search_cache;
     513    private int size;
     514
    549515    public QuadBuckets()
    550516    {
    551517        clear();
    552518    }
    553     public void clear()
    554     {
    555         root = new QBLevel();
    556         search_cache = null;
    557         if (debug) {
    558             out("QuadBuckets() cleared: " + this);
    559             out("root: " + root + " level: " + root.level + " bbox: " + root.bbox());
    560         }
    561     }
    562     public boolean add(T n)
    563     {
    564 
    565         root.add(n);
    566         return true;
     519    public void clear()  {
     520        synchronized (split_lock) {
     521            root = new QBLevel();
     522            search_cache = null;
     523            size = 0;
     524            if (debug) {
     525                out("QuadBuckets() cleared: " + this);
     526                out("root: " + root + " level: " + root.level + " bbox: " + root.bbox());
     527            }
     528        }
     529    }
     530    public boolean add(T n) {
     531        synchronized (split_lock) {
     532            root.add(n);
     533            size++;
     534            return true;
     535        }
    567536    }
    568537    public void unsupported()
     
    613582        return (T)raw;
    614583    }
    615     public boolean remove(Object o)
    616     {
     584    public boolean remove(Object o) {
    617585        return this.remove(convert(o));
    618586    }
    619587    public boolean remove(T o) {
    620         search_cache = null; // Search cache might point to one of removed buckets
    621         QBLevel bucket = root.findBucket(o.getBBox());
    622         return bucket.remove_content(o);
     588        synchronized (split_lock) {
     589            search_cache = null; // Search cache might point to one of removed buckets
     590            QBLevel bucket = root.findBucket(o.getBBox());
     591            if (bucket.remove_content(o)) {
     592                size--;
     593                return true;
     594            } else
     595                return false;
     596        }
    623597    }
    624598    public boolean contains(Object o) {
     
    748722        return new QuadBucketIterator(this);
    749723    }
    750     public int size()
    751     {
    752         // This can certainly by optimized
    753         int ret = root.size();
    754         if (debug) {
    755             out(this + " QuadBuckets size: " + ret);
    756         }
    757         return ret;
    758     }
    759     public int size_iter()
    760     {
    761         int count = 0;
    762         Iterator<T> i = this.iterator();
    763         while (i.hasNext()) {
    764             i.next();
    765             count++;
    766         }
    767         return count;
    768     }
     724    public int size() {
     725        synchronized (split_lock) {
     726            return size;
     727        }
     728    }
     729
    769730    public boolean isEmpty()
    770731    {
Note: See TracChangeset for help on using the changeset viewer.