0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

Add generate helper on kdtree module.

That uses lru cache for speedup tree creation
for most used configurations.
This commit is contained in:
Andrey Antukh 2016-06-11 19:48:39 +03:00
parent 4ab20eaef0
commit 8e84751181
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

21
vendor/kdtree/core.js vendored
View file

@ -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;