Skip to content

Optimized quad tree and cluster algorithm with HashSet instead of ArrayList #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NonHierarchicalDistanceBasedAlgorithm<T extends ClusterItem> implem
/**
* Any modifications should be synchronized on mQuadTree.
*/
private final Collection<QuadItem<T>> mItems = new ArrayList<QuadItem<T>>();
private final Collection<QuadItem<T>> mItems = new HashSet<>();

/**
* Any modifications should be synchronized on mQuadTree.
Expand Down
10 changes: 6 additions & 4 deletions library/src/com/google/maps/android/quadtree/PointQuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A quad tree which tracks items with a Point geometry.
Expand Down Expand Up @@ -50,7 +52,7 @@ public interface Item {
/**
* The elements inside this quad, if any.
*/
private List<T> mItems;
private Set<T> mItems;

/**
* Maximum depth.
Expand Down Expand Up @@ -115,7 +117,7 @@ private void insert(double x, double y, T item) {
return;
}
if (mItems == null) {
mItems = new ArrayList<T>();
mItems = new HashSet<>();
}
mItems.add(item);
if (mItems.size() > MAX_ELEMENTS && mDepth < MAX_DEPTH) {
Expand All @@ -133,7 +135,7 @@ private void split() {
mChildren.add(new PointQuadTree<T>(mBounds.minX, mBounds.midX, mBounds.midY, mBounds.maxY, mDepth + 1));
mChildren.add(new PointQuadTree<T>(mBounds.midX, mBounds.maxX, mBounds.midY, mBounds.maxY, mDepth + 1));

List<T> items = mItems;
Set<T> items = mItems;
mItems = null;

for (T item : items) {
Expand Down Expand Up @@ -211,7 +213,7 @@ private void search(Bounds searchBounds, Collection<T> results) {
}
} else if (mItems != null) {
if (searchBounds.contains(mBounds)) {
results.addAll(mItems);
results.addAll(mItems);
} else {
for (T item : mItems) {
if (searchBounds.contains(item.getPoint())) {
Expand Down