diff options
author | alon@honor <none@none> | 2010-10-17 23:15:33 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-10-17 23:15:33 -0700 |
commit | 7439185557cae67f03a44a3070cceb4c45f4e1a7 (patch) | |
tree | a8de622ec6be09e0a06c1d77620cf14b56e8a99d /src/parseTools.js | |
parent | 04dff51782dd5e8bb1a209e93d39499c59b190fa (diff) |
optimize reintegrator
Diffstat (limited to 'src/parseTools.js')
-rw-r--r-- | src/parseTools.js | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index 3ee56ece..fe8d1159 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -203,29 +203,32 @@ function splitItem(parent, childSlot, copySlots) { } function makeReintegrator(afterFunc) { - // reintegration - find intermediate representation-parsed items and - // place back in parents TODO: Optimize this code to optimal O(..) + // Reintegration - find intermediate representation-parsed items and + // place back in parents return { process: function(items) { var ret = []; + var lineDict = {}; for (var i = 0; i < items.length; i++) { - var found = false; - if (items[i] && items[i].parentSlot) { - var child = items[i]; - for (var j = 0; j < items.length; j++) { - if (items[j] && items[j].lineNum == items[i].parentLineNum) { - var parent = items[j]; - // process the pair - parent[child.parentSlot] = child; - delete child.parentLineNum; - afterFunc.call(this, parent, child); - - items[i] = null; - items[j] = null; - found = true; - break; - } - } + var item = items[i]; + if (!item.parentSlot) { + assert(!lineDict[item.lineNum]); + lineDict[item.lineNum] = i; + } + } + for (var i = 0; i < items.length; i++) { + var child = items[i]; + var j = lineDict[child.parentLineNum]; + if (typeof j === 'number') { + var parent = items[j]; + // process the pair + parent[child.parentSlot] = child; + delete child.parentLineNum; + afterFunc.call(this, parent, child); + + items[i] = null; + items[j] = null; + lineDict[child.parentLineNum] = null; } } this.forwardItems(items.filter(function(item) { return !!item }), this.name_); // next time hopefully |