From 74c7607b3c3ec9cb8e90796407ad003d6ed21dfb Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 28 Nov 2017 11:15:47 +0200 Subject: [PATCH 1/4] Replace recursion with loop --- OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index fdb2b315ef..f99e0bd8f5 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -458,7 +458,8 @@ public class MapMarkersDbHelper { } private void buildLinkedList(HashMap markers, List res) { - if (!markers.isEmpty()) { + int markersCount = markers.size(); + for (int i = 0; i < markersCount; i++) { int count = 1; for (MapMarker marker : markers.values()) { if (!markers.keySet().contains(marker.nextKey) || count == markers.size()) { @@ -468,7 +469,6 @@ public class MapMarkersDbHelper { } count++; } - buildLinkedList(markers, res); } } From 57b7081c682fba1a3a521af631eeeb42ea5954a1 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 28 Nov 2017 11:51:38 +0200 Subject: [PATCH 2/4] Simplify code --- .../net/osmand/plus/mapmarkers/MapMarkersDbHelper.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index f99e0bd8f5..6ff93d31ea 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -406,7 +406,6 @@ public class MapMarkersDbHelper { } public List getActiveMarkers() { - List res = new LinkedList<>(); HashMap markers = new LinkedHashMap<>(); SQLiteConnection db = openConnection(true); if (db != null) { @@ -423,9 +422,8 @@ public class MapMarkersDbHelper { } finally { db.close(); } - buildLinkedList(markers, res); } - return res; + return buildLinkedList(markers); } private MapMarker readItem(SQLiteCursor query) { @@ -457,12 +455,13 @@ public class MapMarkersDbHelper { return marker; } - private void buildLinkedList(HashMap markers, List res) { + private List buildLinkedList(HashMap markers) { + List res = new LinkedList<>(); int markersCount = markers.size(); for (int i = 0; i < markersCount; i++) { int count = 1; for (MapMarker marker : markers.values()) { - if (!markers.keySet().contains(marker.nextKey) || count == markers.size()) { + if (!markers.containsKey(marker.nextKey) || count == markers.size()) { res.add(0, marker); markers.remove(marker.id); break; @@ -470,6 +469,7 @@ public class MapMarkersDbHelper { count++; } } + return res; } public void updateMarker(MapMarker marker) { From 277736de35872a07e9406948f633a0913c151399 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 28 Nov 2017 14:20:08 +0200 Subject: [PATCH 3/4] Replace implementation with interface --- .../src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index 6ff93d31ea..fb97727f91 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -13,10 +13,10 @@ import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.helpers.SearchHistoryHelper; import java.util.Calendar; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Random; public class MapMarkersDbHelper { @@ -406,7 +406,7 @@ public class MapMarkersDbHelper { } public List getActiveMarkers() { - HashMap markers = new LinkedHashMap<>(); + Map markers = new LinkedHashMap<>(); SQLiteConnection db = openConnection(true); if (db != null) { try { @@ -455,7 +455,7 @@ public class MapMarkersDbHelper { return marker; } - private List buildLinkedList(HashMap markers) { + private List buildLinkedList(Map markers) { List res = new LinkedList<>(); int markersCount = markers.size(); for (int i = 0; i < markersCount; i++) { From 9454b6b35abe849762d121457a1c48e98578c64a Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Mon, 4 Dec 2017 17:41:43 +0200 Subject: [PATCH 4/4] Fix #4676 --- .../plus/mapmarkers/MapMarkersDbHelper.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index fb97727f91..3cab3d6b15 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -12,12 +12,16 @@ import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.helpers.SearchHistoryHelper; +import java.util.ArrayList; import java.util.Calendar; +import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.Set; public class MapMarkersDbHelper { @@ -407,6 +411,7 @@ public class MapMarkersDbHelper { public List getActiveMarkers() { Map markers = new LinkedHashMap<>(); + Set nextKeys = new HashSet<>(); SQLiteConnection db = openConnection(true); if (db != null) { try { @@ -416,6 +421,7 @@ public class MapMarkersDbHelper { do { MapMarker marker = readItem(query); markers.put(marker.id, marker); + nextKeys.add(marker.nextKey); } while (query.moveToNext()); } query.close(); @@ -423,7 +429,7 @@ public class MapMarkersDbHelper { db.close(); } } - return buildLinkedList(markers); + return buildLinkedList(markers, nextKeys); } private MapMarker readItem(SQLiteCursor query) { @@ -455,20 +461,31 @@ public class MapMarkersDbHelper { return marker; } - private List buildLinkedList(Map markers) { - List res = new LinkedList<>(); - int markersCount = markers.size(); - for (int i = 0; i < markersCount; i++) { - int count = 1; - for (MapMarker marker : markers.values()) { - if (!markers.containsKey(marker.nextKey) || count == markers.size()) { - res.add(0, marker); - markers.remove(marker.id); + private List buildLinkedList(Map markers, Set nextKeys) { + List res = new ArrayList<>(markers.size()); + + while (!markers.isEmpty()) { + MapMarker head = null; + + Iterator iterator = markers.values().iterator(); + while (iterator.hasNext()) { + MapMarker marker = iterator.next(); + if (!nextKeys.contains(marker.id) || !iterator.hasNext()) { + head = marker; break; } - count++; } + + if (head == null) { + break; + } + + do { + res.add(head); + markers.remove(head.id); + } while ((head = markers.get(head.nextKey)) != null); } + return res; }