Skip to content

Commit

Permalink
perf: migrate rbTree to js-sdsl (#16267)
Browse files Browse the repository at this point in the history
* 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 38e8171
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions lib/rules/indent.js
Expand Up @@ -12,7 +12,7 @@
// Requirements
//------------------------------------------------------------------------------

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

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

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

/**
Expand All @@ -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);
}

/**
Expand All @@ -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] };
}

/**
Expand All @@ -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);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 38e8171

Please sign in to comment.