Optimize difference and parentWays

This commit is contained in:
Tom MacWright
2013-01-09 13:45:04 -05:00
parent 5c775ce251
commit 850e7d6568
2 changed files with 18 additions and 11 deletions
+10 -5
View File
@@ -46,15 +46,20 @@ iD.Graph.prototype = {
},
parentWays: function(id) {
// This is slow and a bad hack.
return _.filter(this.entities, function(e) {
return e && e.type === 'way' && e.nodes.indexOf(id) !== -1;
});
var o = [];
for (var i in this.entities) {
if (this.entities[i] &&
this.entities[i].type === 'way' &&
this.entities[i].nodes.indexOf(id) !== -1) {
o.push(this.entities[i]);
}
}
return o;
},
parentRelations: function(id) {
// This is slow and a bad hack.
return _.filter(this.entities, function(e) {
return _.filter(this.entities, function buildParentRelations(e) {
return e && e.type === 'relation' &&
_.pluck(e.members, 'id').indexOf(id) !== -1;
});
+8 -6
View File
@@ -75,17 +75,19 @@ iD.Map = function() {
filter = d3.functor(true);
} else {
var only = {};
difference.forEach(function buildDifference(id) {
for (var j = 0; j < difference.length; j++) {
var id = difference[j];
only[id] = graph.fetch(id);
if (only[id] && only[id].type === 'node') {
graph.parentWays(id).forEach(function buildOnly(parent) {
var parents = graph.parentWays(id);
for (var k = 0; k < parents.length; k++) {
// Don't re-fetch parents
if (only[parent.id] === undefined) {
only[parent.id] = graph.fetch(parent.id);
if (only[parents[k].id] === undefined) {
only[parent.id] = graph.fetch(parents[k].id);
}
});
}
}
});
}
all = _.compact(_.values(only));
filter = function(d) { return d.accuracy ? d.way in only : d.id in only; };
}