Skip to content
Permalink
Browse files
perf: migrate rbTree to js-sdsl (#16267)
* perf: migrate rbTree to js-sdsl

* fix: keep the old behaviour and change name to ordered map

* feat: upgrade js-sdsl
  • Loading branch information
ZLY201 committed Sep 12, 2022
1 parent 16cba3f commit 38e8171d9b170f400ac340368d044b2093114e94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
@@ -12,7 +12,7 @@
// Requirements
//------------------------------------------------------------------------------

const createTree = require("functional-red-black-tree");
const { OrderedMap } = require("js-sdsl");

const astUtils = require("./utils/ast-utils");

@@ -135,7 +135,8 @@ class BinarySearchTree {
* Creates an empty tree
*/
constructor() {
this._rbTree = createTree();
this._orderedMap = new OrderedMap();
this._orderedMapEnd = this._orderedMap.end();
}

/**
@@ -145,13 +146,7 @@ class BinarySearchTree {
* @returns {void}
*/
insert(key, value) {
const iterator = this._rbTree.find(key);

if (iterator.valid) {
this._rbTree = iterator.update(value);
} else {
this._rbTree = this._rbTree.insert(key, value);
}
this._orderedMap.setElement(key, value);
}

/**
@@ -160,9 +155,13 @@ class BinarySearchTree {
* @returns {{key: number, value: *}|null} The found entry, or null if no such entry exists.
*/
findLe(key) {
const iterator = this._rbTree.le(key);
const iterator = this._orderedMap.reverseLowerBound(key);

return iterator && { key: iterator.key, value: iterator.value };
if (iterator.equals(this._orderedMapEnd)) {
return {};
}

return { key: iterator.pointer[0], value: iterator.pointer[1] };
}

/**
@@ -177,11 +176,20 @@ class BinarySearchTree {
if (start === end) {
return;
}
const iterator = this._rbTree.ge(start);
const iterator = this._orderedMap.lowerBound(start);

while (iterator.valid && iterator.key < end) {
this._rbTree = this._rbTree.remove(iterator.key);
iterator.next();
if (iterator.equals(this._orderedMapEnd)) {
return;
}

if (end > this._orderedMap.back()[0]) {
while (!iterator.equals(this._orderedMapEnd)) {
this._orderedMap.eraseElementByIterator(iterator);
}
} else {
while (iterator.pointer[0] < end) {
this._orderedMap.eraseElementByIterator(iterator);
}
}
}
}
@@ -74,7 +74,6 @@
"fast-deep-equal": "^3.1.3",
"file-entry-cache": "^6.0.1",
"find-up": "^5.0.0",
"functional-red-black-tree": "^1.0.1",
"glob-parent": "^6.0.1",
"globals": "^13.15.0",
"globby": "^11.1.0",
@@ -83,6 +82,7 @@
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
"is-glob": "^4.0.0",
"js-sdsl": "^4.1.4",
"js-yaml": "^4.1.0",
"json-stable-stringify-without-jsonify": "^1.0.1",
"levn": "^0.4.1",

0 comments on commit 38e8171

Please sign in to comment.