use specialized BinarySort for MapTiles that also repacks the 'sparsed' array
This commit is contained in:
@@ -17,8 +17,6 @@ package org.oscim.generator;
|
||||
import static org.oscim.generator.JobTile.STATE_LOADING;
|
||||
import static org.oscim.generator.JobTile.STATE_NONE;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A JobQueue keeps the list of pending jobs for a MapView and prioritizes them.
|
||||
*/
|
||||
@@ -71,8 +69,12 @@ public class JobQueue {
|
||||
if (mJobs == null)
|
||||
return null;
|
||||
|
||||
if (mCurrentJob == 0)
|
||||
Arrays.sort(mJobs);
|
||||
if (mCurrentJob == 0) {
|
||||
//Arrays.sort(mJobs);
|
||||
int len = mJobs.length;
|
||||
if (len > 1)
|
||||
TileDistanceSort.sort(mJobs, 0, len);
|
||||
}
|
||||
|
||||
//return mPriorityQueue.poll();
|
||||
JobTile t = mJobs[mCurrentJob];
|
||||
|
||||
@@ -21,7 +21,7 @@ import android.util.Log;
|
||||
/**
|
||||
* @author Hannes Janetzek
|
||||
*/
|
||||
public class JobTile extends Tile implements Comparable<JobTile> {
|
||||
public class JobTile extends Tile {
|
||||
private final static String TAG = JobTile.class.getName();
|
||||
|
||||
public final static int STATE_NONE = 0;
|
||||
@@ -59,15 +59,4 @@ public class JobTile extends Tile implements Comparable<JobTile> {
|
||||
public JobTile(int tileX, int tileY, byte zoomLevel) {
|
||||
super(tileX, tileY, zoomLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(JobTile o) {
|
||||
if (this.distance < o.distance) {
|
||||
return -1;
|
||||
}
|
||||
if (this.distance > o.distance) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
* Copyright 2013 OpenScienceMap
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
@@ -14,19 +14,109 @@
|
||||
*/
|
||||
package org.oscim.generator;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* based on ComparableTimSort:
|
||||
* everything below is Copyright OpenJDK, Oracle
|
||||
*/
|
||||
public class TileDistanceSort implements Comparator<JobTile> {
|
||||
|
||||
@Override
|
||||
public int compare(JobTile tile1, JobTile tile2) {
|
||||
if (tile1.distance == tile2.distance)
|
||||
public class TileDistanceSort {
|
||||
public static void sort(JobTile[] a, int lo, int hi) {
|
||||
int nRemaining = hi - lo;
|
||||
if (nRemaining < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
int initRunLen = countRunAndMakeAscending(a, lo, hi);
|
||||
binarySort(a, lo, hi, lo + initRunLen);
|
||||
|
||||
}
|
||||
|
||||
static int compareTo(JobTile a, JobTile b) {
|
||||
if (a == null && b == null)
|
||||
return 0;
|
||||
|
||||
return tile1.distance > tile2.distance ? 1 : -1;
|
||||
if (a == null)
|
||||
return 1;
|
||||
|
||||
if (b == null)
|
||||
return -1;
|
||||
|
||||
if (a.distance < b.distance) {
|
||||
return -1;
|
||||
}
|
||||
if (a.distance > b.distance) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void binarySort(JobTile[] a, int lo, int hi, int start)
|
||||
{
|
||||
assert ((lo <= start) && (start <= hi));
|
||||
if (start == lo)
|
||||
++start;
|
||||
for (; start < hi; ++start)
|
||||
{
|
||||
JobTile pivot = a[start];
|
||||
|
||||
int left = lo;
|
||||
int right = start;
|
||||
assert (left <= right);
|
||||
|
||||
while (left < right) {
|
||||
int mid = left + right >>> 1;
|
||||
//if (pivot.compareTo(a[mid]) < 0)
|
||||
if (compareTo(pivot, a[mid]) < 0)
|
||||
right = mid;
|
||||
else
|
||||
left = mid + 1;
|
||||
}
|
||||
assert (left == right);
|
||||
|
||||
int n = start - left;
|
||||
|
||||
switch (n)
|
||||
{
|
||||
case 2:
|
||||
a[(left + 2)] = a[(left + 1)];
|
||||
//$FALL-THROUGH$
|
||||
case 1:
|
||||
a[(left + 1)] = a[left];
|
||||
break;
|
||||
default:
|
||||
System.arraycopy(a, left, a, left + 1, n);
|
||||
}
|
||||
a[left] = pivot;
|
||||
}
|
||||
}
|
||||
|
||||
private static int countRunAndMakeAscending(JobTile[] a, int lo, int hi)
|
||||
{
|
||||
assert (lo < hi);
|
||||
int runHi = lo + 1;
|
||||
if (runHi == hi) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (compareTo((a[(runHi++)]), a[lo]) < 0) {
|
||||
while ((runHi < hi) && (compareTo((a[runHi]), a[(runHi - 1)]) < 0))
|
||||
++runHi;
|
||||
reverseRange(a, lo, runHi);
|
||||
} else {
|
||||
while ((runHi < hi) && (compareTo((a[runHi]), a[(runHi - 1)]) >= 0)) {
|
||||
++runHi;
|
||||
}
|
||||
}
|
||||
return (runHi - lo);
|
||||
}
|
||||
|
||||
private static void reverseRange(JobTile[] a, int lo, int hi)
|
||||
{
|
||||
--hi;
|
||||
while (lo < hi) {
|
||||
JobTile t = a[lo];
|
||||
a[(lo++)] = a[hi];
|
||||
a[(hi--)] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user