From a08dd7ecce839401d40d6b866b30bc0c0ce0976a Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 11 Jan 2013 19:14:17 -0500 Subject: [PATCH 1/2] More efficient parentways --- js/id/graph/graph.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 868c17de5..4729bd101 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -36,19 +36,27 @@ iD.Graph.prototype = { }, parentWays: function(entity) { - var graph = this; - return this.transient(entity, 'parentWays', - function generateParentWays() { - var o = []; + var graph = this, + ent, + node; + + if (!graph.calculatedParentWays) { for (var i in graph.entities) { - if (graph.entities[i] && - graph.entities[i].type === 'way' && - graph.entities[i].nodes.indexOf(this.id) !== -1) { - o.push(graph.entities[i]); + ent = graph.entities[i]; + if (ent && ent.type === 'way') { + for (var j = 0; j < ent.nodes.length; j++) { + node = graph.entities[ent.nodes[j]]; + node.parentways = node.parentways || []; + if (node.parentways.indexOf(ent) < 0) { + node.parentways.push(ent); + } + } } } - return o; - }); + graph.calculatedParentWays = true; + } + + return entity.parentways || []; }, parentRelations: function(entity) { From 62e78b5b9b9adaeea033e1fa737144e8f08893e1 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 11 Jan 2013 20:37:32 -0500 Subject: [PATCH 2/2] clean up parentWays --- js/id/graph/graph.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 4729bd101..b6d0485b3 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -11,6 +11,7 @@ iD.Graph = function(entities) { } this.transients = {}; + this._parentWays = {}; if (iD.debug) { Object.freeze(this); @@ -37,18 +38,18 @@ iD.Graph.prototype = { parentWays: function(entity) { var graph = this, - ent, - node; + entity, + id; if (!graph.calculatedParentWays) { for (var i in graph.entities) { - ent = graph.entities[i]; - if (ent && ent.type === 'way') { - for (var j = 0; j < ent.nodes.length; j++) { - node = graph.entities[ent.nodes[j]]; - node.parentways = node.parentways || []; - if (node.parentways.indexOf(ent) < 0) { - node.parentways.push(ent); + entity = graph.entities[i]; + if (entity && entity.type === 'way') { + for (var j = 0; j < entity.nodes.length; j++) { + id = entity.nodes[j]; + this._parentWays[id] = this._parentWays[id] || []; + if (this._parentWays[id].indexOf(entity) < 0) { + this._parentWays[id].push(entity); } } } @@ -56,7 +57,7 @@ iD.Graph.prototype = { graph.calculatedParentWays = true; } - return entity.parentways || []; + return this._parentWays[entity.id] || []; }, parentRelations: function(entity) {