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:
parent
4ab20eaef0
commit
8e84751181
1 changed files with 19 additions and 2 deletions
21
vendor/kdtree/core.js
vendored
21
vendor/kdtree/core.js
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue