Merge pull request #72 from arlas/patch-7

optimizations from http://developer.android.com/guide/practices/design/pe
This commit is contained in:
pavolzibrita 2011-09-19 21:32:28 -07:00
commit eabcf13509
8 changed files with 50 additions and 44 deletions

View file

@ -133,7 +133,8 @@ public class BinaryInspector {
BinaryIndexPart part = indexes[c].getIndexes().get(i); BinaryIndexPart part = indexes[c].getIndexes().get(i);
if(part instanceof MapIndex){ if(part instanceof MapIndex){
List<MapRoot> roots = ((MapIndex) part).getRoots(); List<MapRoot> roots = ((MapIndex) part).getRoots();
for(int j=0; j<roots.size(); j++){ int rsize = roots.size();
for(int j=0; j<rsize; j++){
partsSet[c].add((i+1f)+(j+1)/10f); partsSet[c].add((i+1f)+(j+1)/10f);
} }
} }
@ -184,7 +185,8 @@ public class BinaryInspector {
List<MapRoot> toSkip = new ArrayList<MapRoot>(); List<MapRoot> toSkip = new ArrayList<MapRoot>();
int newL = 0; int newL = 0;
int tagAndFieldSize = CodedOutputStream.computeTagSize(OsmandOdb.OsmAndMapIndex.LEVELS_FIELD_NUMBER) + 4; int tagAndFieldSize = CodedOutputStream.computeTagSize(OsmandOdb.OsmAndMapIndex.LEVELS_FIELD_NUMBER) + 4;
for(int j=0; j<roots.size(); j++){ int rsize = roots.size();
for(int j=0; j<rsize; j++){
if (!partSet.contains(i + 1f + (j+1)*0.1f)) { if (!partSet.contains(i + 1f + (j+1)*0.1f)) {
newL -= (roots.get(j).getLength() + tagAndFieldSize); newL -= (roots.get(j).getLength() + tagAndFieldSize);
toSkip.add(roots.get(j)); toSkip.add(roots.get(j));

View file

@ -17,13 +17,14 @@ public class MapAlgorithms {
} }
ArrayList<Integer> l = new ArrayList<Integer>(); ArrayList<Integer> l = new ArrayList<Integer>();
int first = 0; int first = 0;
while(first < n.size()){ int nsize = n.size();
while(first < nsize){
if(n.get(first) != null){ if(n.get(first) != null){
break; break;
} }
first++; first++;
} }
int last = n.size() - 1; int last = nsize - 1;
while (last >= 0) { while (last >= 0) {
if (n.get(last) != null) { if (n.get(last) != null) {
break; break;
@ -54,7 +55,8 @@ public class MapAlgorithms {
} }
simplifyDouglasPeucker(n, zoom, epsilon, l, first, last); simplifyDouglasPeucker(n, zoom, epsilon, l, first, last);
w.addNode(n.get(first)); w.addNode(n.get(first));
for (int i = 0; i < l.size(); i++) { int lsize = l.size();
for (int i = 0; i < lsize; i++) {
w.addNode(n.get(l.get(i))); w.addNode(n.get(l.get(i)));
} }
if (cycle) { if (cycle) {
@ -99,7 +101,6 @@ public class MapAlgorithms {
public static boolean isClockwiseWay(Way w){ public static boolean isClockwiseWay(Way w){
return isClockwiseWay(Collections.singletonList(w)); return isClockwiseWay(Collections.singletonList(w));
} }
public static boolean isClockwiseWay(List<Way> ways){ public static boolean isClockwiseWay(List<Way> ways){
@ -120,12 +121,13 @@ public class MapAlgorithms {
for(Way w : ways){ for(Way w : ways){
List<Node> ns = w.getNodes(); List<Node> ns = w.getNodes();
int startInd = 0; int startInd = 0;
if(firstWay && ns.size() > 0){ int nssize = ns.size();
if(firstWay && nssize > 0){
prev = ns.get(0); prev = ns.get(0);
startInd = 1; startInd = 1;
firstWay = false; firstWay = false;
} }
for(int i = startInd; i < ns.size();i++) { for(int i = startInd; i < nssize;i++) {
Node next = ns.get(i); Node next = ns.get(i);
double rlon = ray_intersect_lon(prev, next, lat, lon); double rlon = ray_intersect_lon(prev, next, lat, lon);
if(rlon != - 360d){ if(rlon != - 360d){
@ -161,8 +163,6 @@ public class MapAlgorithms {
} }
return clockwiseSum >= 0; return clockwiseSum >= 0;
} }
// try to intersect from left to right // try to intersect from left to right
@ -194,8 +194,5 @@ public class MapAlgorithms {
} }
} }
} }
} }
}
}

View file

@ -8,7 +8,6 @@ import net.osmand.osm.MapUtils;
import net.osmand.osm.Relation; import net.osmand.osm.Relation;
import net.osmand.osm.Way; import net.osmand.osm.Way;
public class TransportRoute extends MapObject { public class TransportRoute extends MapObject {
private List<Way> ways; private List<Way> ways;
private List<TransportStop> forwardStops = new ArrayList<TransportStop>(); private List<TransportStop> forwardStops = new ArrayList<TransportStop>();
@ -85,14 +84,14 @@ public class TransportRoute extends MapObject {
public int getAvgBothDistance(){ public int getAvgBothDistance(){
int d = 0; int d = 0;
for(int i=1; i< backwardStops.size(); i++){ int bSsize = backwardStops.size();
int fSsize = forwardStops.size();
for(int i=1; i< bSsize; i++){
d += MapUtils.getDistance(backwardStops.get(i-1).getLocation(), backwardStops.get(i).getLocation()); d += MapUtils.getDistance(backwardStops.get(i-1).getLocation(), backwardStops.get(i).getLocation());
} }
for(int i=1; i< forwardStops.size(); i++){ for(int i=1; i< fSsize; i++){
d += MapUtils.getDistance(forwardStops.get(i-1).getLocation(), forwardStops.get(i).getLocation()); d += MapUtils.getDistance(forwardStops.get(i-1).getLocation(), forwardStops.get(i).getLocation());
} }
return d; return d;
} }
}
}

View file

@ -834,13 +834,14 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{
} }
int j = 0; int j = 0;
for (; j < cities.size(); j++) { int csize = cities.size();
for (; j < csize; j++) {
City c = cities.get(j); City c = cities.get(j);
if (c.getType() != CityType.CITY && c.getType() != CityType.TOWN) { if (c.getType() != CityType.CITY && c.getType() != CityType.TOWN) {
break; break;
} }
} }
progress.startTask(Messages.getString("IndexCreator.SERIALIZING_ADRESS"), j + ((cities.size() - j) / 100 + 1)); //$NON-NLS-1$ progress.startTask(Messages.getString("IndexCreator.SERIALIZING_ADRESS"), j + ((csize - j) / 100 + 1)); //$NON-NLS-1$
Map<String, Set<Street>> postcodes = new TreeMap<String, Set<Street>>(); Map<String, Set<Street>> postcodes = new TreeMap<String, Set<Street>>();
boolean writeCities = true; boolean writeCities = true;
@ -855,7 +856,7 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{
// write cities and after villages // write cities and after villages
writer.startCityIndexes(false); writer.startCityIndexes(false);
for (int i = 0; i < cities.size(); i++) { for (int i = 0; i < csize; i++) {
City c = cities.get(i); City c = cities.get(i);
List<City> listSuburbs = null; List<City> listSuburbs = null;
for (City suburb : suburbs) { for (City suburb : suburbs) {
@ -868,7 +869,7 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{
} }
if (writeCities) { if (writeCities) {
progress.progress(1); progress.progress(1);
} else if ((cities.size() - i) % 100 == 0) { } else if ((csize - i) % 100 == 0) {
progress.progress(1); progress.progress(1);
} }
if (writeCities && c.getType() != CityType.CITY && c.getType() != CityType.TOWN) { if (writeCities && c.getType() != CityType.CITY && c.getType() != CityType.TOWN) {
@ -1130,7 +1131,4 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{
stat.close(); stat.close();
return cities; return cities;
} }
} }

View file

@ -282,7 +282,8 @@ public class IndexVectorMapCreator extends AbstractIndexPartCreator {
List<Way> l = new ArrayList<Way>(); List<Way> l = new ArrayList<Way>();
l.add(w); l.add(w);
boolean add = true; boolean add = true;
for (int k = 0; k < incompletedRings.size();) { int iRsize = incompletedRings.size();
for (int k = 0; k < iRsize;) {
boolean remove = false; boolean remove = false;
List<Way> i = incompletedRings.get(k); List<Way> i = incompletedRings.get(k);
Way last = i.get(i.size() - 1); Way last = i.get(i.size() - 1);
@ -576,7 +577,8 @@ public class IndexVectorMapCreator extends AbstractIndexPartCreator {
fs.close(); fs.close();
} }
List<Node> wNodes = new ArrayList<Node>(); List<Node> wNodes = new ArrayList<Node>();
for (int i = 0; i < wayNodes.size(); i += 2) { int wNsize = wayNodes.size();
for (int i = 0; i < wNsize; i += 2) {
wNodes.add(new Node(wayNodes.get(i), wayNodes.get(i + 1), i == 0 ? startNode : endNode)); wNodes.add(new Node(wayNodes.get(i), wayNodes.get(i + 1), i == 0 ? startNode : endNode));
} }
boolean skip = false; boolean skip = false;
@ -605,7 +607,8 @@ public class IndexVectorMapCreator extends AbstractIndexPartCreator {
int minY = Integer.MAX_VALUE; int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE; int maxY = Integer.MIN_VALUE;
int c = 0; int c = 0;
for (int i = 0; i < nodes.size(); i++) { int nsize = nodes.size();
for (int i = 0; i < nsize; i++) {
if (nodes.get(i) != null) { if (nodes.get(i) != null) {
c++; c++;
int x = (int) (MapUtils.getTileNumberX(zoom, nodes.get(i).getLongitude()) * 256d); int x = (int) (MapUtils.getTileNumberX(zoom, nodes.get(i).getLongitude()) * 256d);

View file

@ -88,11 +88,11 @@ public class Way extends Entity {
} else { } else {
nodes.clear(); nodes.clear();
} }
for (int i = 0; i < nodeIds.size(); i++) { int nIsize = nodeIds.size();
for (int i = 0; i < nIsize; i++) {
nodes.add((Node) entities.get(new EntityId(EntityType.NODE,nodeIds.get(i)))); nodes.add((Node) entities.get(new EntityId(EntityType.NODE,nodeIds.get(i))));
} }
} }
} }
@Override @Override

View file

@ -385,7 +385,8 @@ public class OsmBaseStorage extends DefaultHandler {
@Override @Override
protected void parseNodes(List<crosby.binary.Osmformat.Node> n) { protected void parseNodes(List<crosby.binary.Osmformat.Node> n) {
EntityInfo info = null; EntityInfo info = null;
for(int i=0; i<n.size(); i++){ int nsize = n.size();
for(int i=0; i<nsize; i++){
crosby.binary.Osmformat.Node nod = n.get(i); crosby.binary.Osmformat.Node nod = n.get(i);
Node e = new Node(parseLat(nod.getLat()), parseLon(nod.getLon()), nod.getId()); Node e = new Node(parseLat(nod.getLat()), parseLon(nod.getLon()), nod.getId());
for(int j=0; j<nod.getKeysCount(); j++){ for(int j=0; j<nod.getKeysCount(); j++){
@ -398,13 +399,14 @@ public class OsmBaseStorage extends DefaultHandler {
} }
registerEntity(EntityType.NODE, e, info); registerEntity(EntityType.NODE, e, info);
} }
updateProgress(n.size()); updateProgress(nsize);
} }
@Override @Override
protected void parseRelations(List<crosby.binary.Osmformat.Relation> r) { protected void parseRelations(List<crosby.binary.Osmformat.Relation> r) {
EntityInfo info = null; EntityInfo info = null;
for(int i=0; i<r.size(); i++){ int rsize = r.size();
for(int i=0; i<rsize; i++){
crosby.binary.Osmformat.Relation rel = r.get(i); crosby.binary.Osmformat.Relation rel = r.get(i);
Relation e = new Relation(rel.getId()); Relation e = new Relation(rel.getId());
long id = 0; long id = 0;
@ -430,13 +432,14 @@ public class OsmBaseStorage extends DefaultHandler {
} }
registerEntity(EntityType.RELATION, e, info); registerEntity(EntityType.RELATION, e, info);
} }
updateProgress(r.size()); updateProgress(rsize);
} }
@Override @Override
protected void parseWays(List<crosby.binary.Osmformat.Way> w) { protected void parseWays(List<crosby.binary.Osmformat.Way> w) {
EntityInfo info = null; EntityInfo info = null;
for(int i=0; i<w.size(); i++){ int wsize = w.size();
for(int i=0; i<wsize; i++){
crosby.binary.Osmformat.Way way = w.get(i); crosby.binary.Osmformat.Way way = w.get(i);
Way e = new Way(way.getId()); Way e = new Way(way.getId());
long id = 0; long id = 0;
@ -454,7 +457,7 @@ public class OsmBaseStorage extends DefaultHandler {
} }
registerEntity(EntityType.WAY, e, info); registerEntity(EntityType.WAY, e, info);
} }
updateProgress(w.size()); updateProgress(wsize);
} }
@Override @Override

View file

@ -126,7 +126,8 @@ public class MinskTransReader {
Map<String, Node> correlated = new LinkedHashMap<String, Node>(); Map<String, Node> correlated = new LinkedHashMap<String, Node>();
Map<Node, String> reverse = new LinkedHashMap<Node, String>(); Map<Node, String> reverse = new LinkedHashMap<Node, String>();
List<TransportStop> stopsToCheck = new ArrayList<TransportStop>(stopsMap.values()); List<TransportStop> stopsToCheck = new ArrayList<TransportStop>(stopsMap.values());
for(int k =0; k<stopsToCheck.size(); k++){ int sTCsize = stopsToCheck.size();
for(int k =0; k<sTCsize; k++){
TransportStop r = stopsToCheck.get(k); TransportStop r = stopsToCheck.get(k);
List<Node> closestObjects = busStops.getClosestObjects(r.latitude, r.longitude, 0, 1); List<Node> closestObjects = busStops.getClosestObjects(r.latitude, r.longitude, 0, 1);
// filter closest objects // filter closest objects
@ -140,7 +141,8 @@ public class MinskTransReader {
MapUtils.sortListOfEntities(closestObjects, r.latitude, r.longitude); MapUtils.sortListOfEntities(closestObjects, r.latitude, r.longitude);
int ind = 0; int ind = 0;
boolean ccorrelated = false; boolean ccorrelated = false;
while(ind < closestObjects.size() && !ccorrelated){ int cOsize = closestObjects.size();
while(ind < cOsize && !ccorrelated){
Node foundNode = closestObjects.get(ind); Node foundNode = closestObjects.get(ind);
if(!reverse.containsKey(foundNode)){ if(!reverse.containsKey(foundNode)){
// all is good no one registered to that stop // all is good no one registered to that stop
@ -279,7 +281,8 @@ public class MinskTransReader {
list.add(0, end); list.add(0, end);
list.add(start); list.add(start);
} }
for(int i=0; i<list.size(); i++){ int lsize = list.size();
for(int i=0; i<lsize; i++){
String st = route.routeStops.get(i); String st = route.routeStops.get(i);
Node correlatedNode = correlated.get(st); Node correlatedNode = correlated.get(st);
TransportStop trStop = trStops.get(st); TransportStop trStop = trStops.get(st);
@ -294,7 +297,7 @@ public class MinskTransReader {
} else if(correlatedNode.getId() != e.getId()){ } else if(correlatedNode.getId() != e.getId()){
double dist = MapUtils.getDistance(correlatedNode, e.getLatLon()); double dist = MapUtils.getDistance(correlatedNode, e.getLatLon());
if(i==list.size() - 1 && !direct && dist < 150){ if(i==lsize - 1 && !direct && dist < 150){
continue; continue;
} }
String eStop = e.getId() + " " + e.getTag(OSMTagKey.NAME); String eStop = e.getId() + " " + e.getTag(OSMTagKey.NAME);
@ -360,7 +363,8 @@ public class MinskTransReader {
Relation relation = checkedRoutes.get(s); Relation relation = checkedRoutes.get(s);
// correlating stops // correlating stops
for (int i = 0; i < r.routeStops.size(); i++) { int rSsize = r.routeStops.size();
for (int i = 0; i < rSsize; i++) {
String stop = r.routeStops.get(i); String stop = r.routeStops.get(i);
if (!stopsMap.containsKey(stop)) { if (!stopsMap.containsKey(stop)) {
throw new IllegalArgumentException("Stops file is not corresponded to routes file"); throw new IllegalArgumentException("Stops file is not corresponded to routes file");
@ -379,7 +383,7 @@ public class MinskTransReader {
System.out.println("[ADD] Added new bus_stop : " + node.getId() + " " + st.name + " minsktrans_stop_id " + st.stopId); System.out.println("[ADD] Added new bus_stop : " + node.getId() + " " + st.name + " minsktrans_stop_id " + st.stopId);
correlated.put(stop, node); correlated.put(stop, node);
} }
if (i == 0 || i == r.routeStops.size() - 1) { if (i == 0 || i == rSsize - 1) {
if (direct) { if (direct) {
relation.addMember(correlated.get(stop).getId(), EntityType.NODE, "stop"); relation.addMember(correlated.get(stop).getId(), EntityType.NODE, "stop");
} }