diff --git a/vendor/kdtree/core.js b/vendor/kdtree/core.js index 5413fc83d..92c2e0724 100644 --- a/vendor/kdtree/core.js +++ b/vendor/kdtree/core.js @@ -14,8 +14,8 @@ "use strict"; goog.provide("kdtree.core"); - goog.require("kdtree.heap"); +goog.require("lru"); goog.require("goog.asserts"); goog.scope(function() { @@ -145,6 +145,7 @@ goog.scope(function() { } // --- Public Api + const cache = new lru.create(); function create(points) { const tree = new KDTree(); @@ -155,6 +156,20 @@ goog.scope(function() { } }; + function generate(width, height, widthStep, heightStep) { + const key = `${width}.${height}.${widthStep}.${heightStep}`; + + let tree = lru.get(cache, key); + if (tree instanceof KDTree) { + return tree; + } else { + tree = new KDTree(); + setup(tree, width, height, widthStep, heightStep); + lru.set(cache, key, tree); + return tree; + } + } + function initialize(tree, points) { assert(goog.isArray(points)); assert(tree instanceof KDTree); @@ -174,7 +189,8 @@ goog.scope(function() { } } - return initialize(tree, points); + initialize(tree, points); + return tree; } function isInitialized(tree) { @@ -197,6 +213,7 @@ goog.scope(function() { // Factory functions kdtree.core.create = create; + kdtree.core.generate = generate; kdtree.core.initialize = initialize; kdtree.core.setup = setup; kdtree.core.isInitialized = isInitialized;