Skip to content
Open
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 @@ -47,6 +47,7 @@ open class NonHierarchicalDistanceBasedAlgorithm<T : ClusterItem> : AbstractAlgo
*/
@JvmField
protected val mItems: MutableCollection<QuadItem<T>> = LinkedHashSet()
protected val mItemMap = HashMap<T, QuadItem<T>>()

/**
* Any modifications should be synchronized on mQuadTree.
Expand All @@ -61,6 +62,7 @@ open class NonHierarchicalDistanceBasedAlgorithm<T : ClusterItem> : AbstractAlgo
synchronized(mQuadTree) {
val result = mItems.add(quadItem)
if (result) {
mItemMap[item] = quadItem
mQuadTree.add(quadItem)
}
return result
Expand All @@ -81,16 +83,17 @@ open class NonHierarchicalDistanceBasedAlgorithm<T : ClusterItem> : AbstractAlgo
override fun clearItems() {
synchronized(mQuadTree) {
mItems.clear()
mItemMap.clear()
mQuadTree.clear()
}
}

override fun removeItem(item: T): Boolean {
// QuadItem delegates hashcode() and equals() to its item so,
// removing any QuadItem to that item will remove the item
val quadItem = QuadItem(item)
synchronized(mQuadTree) {
val result = mItems.remove(quadItem)
val quadItem = mItemMap.remove(item)
val result = quadItem != null && mItems.remove(quadItem)
if (result) {
mQuadTree.remove(quadItem)
}
Expand All @@ -104,8 +107,8 @@ open class NonHierarchicalDistanceBasedAlgorithm<T : ClusterItem> : AbstractAlgo
for (item in items) {
// QuadItem delegates hashcode() and equals() to its item so,
// removing any QuadItem to that item will remove the item
val quadItem = QuadItem(item)
val individualResult = mItems.remove(quadItem)
val quadItem = mItemMap.remove(item)
val individualResult = quadItem != null && mItems.remove(quadItem)
if (individualResult) {
mQuadTree.remove(quadItem)
result = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testInsertionOrder() {
}

private static class TestingItem implements ClusterItem {
private final LatLng mPosition;
private LatLng mPosition;
private String mTitle;

TestingItem(String title, double lat, double lng) {
Expand All @@ -110,6 +110,9 @@ private static class TestingItem implements ClusterItem {
mTitle = "";
mPosition = new LatLng(lat, lng);
}
public void setPosition(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}

@NonNull
@Override
Expand Down