mqtt and socket added

This commit is contained in:
Miisa Ekholm
2022-10-21 11:17:48 +03:00
parent 52370df9ce
commit 9e3d953b9e
751 changed files with 185105 additions and 153 deletions

View File

@@ -0,0 +1,11 @@
import TreeNode from './TreeNode';
import { ContainerIterator } from "../../ContainerBase/index";
declare abstract class TreeIterator<K, V> extends ContainerIterator<K | [K, V]> {
protected node: TreeNode<K, V>;
protected header: TreeNode<K, V>;
pre: () => this;
next: () => this;
constructor(node: TreeNode<K, V>, header: TreeNode<K, V>, iteratorType?: boolean);
equals(obj: TreeIterator<K, V>): boolean;
}
export default TreeIterator;

View File

@@ -0,0 +1,62 @@
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { ContainerIterator } from "../../ContainerBase/index";
var TreeIterator = /** @class */ (function (_super) {
__extends(TreeIterator, _super);
function TreeIterator(node, header, iteratorType) {
var _this = _super.call(this, iteratorType) || this;
_this.node = node;
_this.header = header;
if (_this.iteratorType === ContainerIterator.NORMAL) {
_this.pre = function () {
if (this.node === this.header.left) {
throw new RangeError('LinkList iterator access denied!');
}
this.node = this.node.pre();
return this;
};
_this.next = function () {
if (this.node === this.header) {
throw new RangeError('LinkList iterator access denied!');
}
this.node = this.node.next();
return this;
};
}
else {
_this.pre = function () {
if (this.node === this.header.right) {
throw new RangeError('LinkList iterator access denied!');
}
this.node = this.node.next();
return this;
};
_this.next = function () {
if (this.node === this.header) {
throw new RangeError('LinkList iterator access denied!');
}
this.node = this.node.pre();
return this;
};
}
return _this;
}
TreeIterator.prototype.equals = function (obj) {
return this.node === obj.node;
};
return TreeIterator;
}(ContainerIterator));
export default TreeIterator;

View File

@@ -0,0 +1,36 @@
declare class TreeNode<K, V> {
static readonly RED = true;
static readonly BLACK = false;
color: boolean;
key: K | undefined;
value: V | undefined;
left: TreeNode<K, V> | undefined;
right: TreeNode<K, V> | undefined;
parent: TreeNode<K, V> | undefined;
constructor(key?: K, value?: V);
/**
* @description Get the pre node.
* @return TreeNode about the pre node.
*/
pre(): TreeNode<K, V>;
/**
* @description Get the next node.
* @return TreeNode about the next node.
*/
next(): TreeNode<K, V>;
/**
* @description Rotate left.
* @return TreeNode about moved to original position after rotation.
*/
rotateLeft(): TreeNode<K, V>;
/**
* @description Rotate left.
* @return TreeNode about moved to original position after rotation.
*/
rotateRight(): TreeNode<K, V>;
/**
* @description Remove this.
*/
remove(): void;
}
export default TreeNode;

View File

@@ -0,0 +1,121 @@
var TreeNode = /** @class */ (function () {
function TreeNode(key, value) {
this.color = true;
this.key = undefined;
this.value = undefined;
this.left = undefined;
this.right = undefined;
this.parent = undefined;
this.key = key;
this.value = value;
}
/**
* @description Get the pre node.
* @return TreeNode about the pre node.
*/
TreeNode.prototype.pre = function () {
var preNode = this;
if (preNode.color === TreeNode.RED &&
preNode.parent.parent === preNode) {
preNode = preNode.right;
}
else if (preNode.left) {
preNode = preNode.left;
while (preNode.right) {
preNode = preNode.right;
}
}
else {
var pre = preNode.parent;
while (pre.left === preNode) {
preNode = pre;
pre = preNode.parent;
}
preNode = pre;
}
return preNode;
};
/**
* @description Get the next node.
* @return TreeNode about the next node.
*/
TreeNode.prototype.next = function () {
var nextNode = this;
if (nextNode.right) {
nextNode = nextNode.right;
while (nextNode.left) {
nextNode = nextNode.left;
}
}
else {
var pre = nextNode.parent;
while (pre.right === nextNode) {
nextNode = pre;
pre = nextNode.parent;
}
if (nextNode.right !== pre) {
nextNode = pre;
}
}
return nextNode;
};
/**
* @description Rotate left.
* @return TreeNode about moved to original position after rotation.
*/
TreeNode.prototype.rotateLeft = function () {
var PP = this.parent;
var V = this.right;
var R = V.left;
if (PP.parent === this)
PP.parent = V;
else if (PP.left === this)
PP.left = V;
else
PP.right = V;
V.parent = PP;
V.left = this;
this.parent = V;
this.right = R;
if (R)
R.parent = this;
return V;
};
/**
* @description Rotate left.
* @return TreeNode about moved to original position after rotation.
*/
TreeNode.prototype.rotateRight = function () {
var PP = this.parent;
var F = this.left;
var K = F.right;
if (PP.parent === this)
PP.parent = F;
else if (PP.left === this)
PP.left = F;
else
PP.right = F;
F.parent = PP;
F.right = this;
this.parent = F;
this.left = K;
if (K)
K.parent = this;
return F;
};
/**
* @description Remove this.
*/
TreeNode.prototype.remove = function () {
var parent = this.parent;
if (this === parent.left) {
parent.left = undefined;
}
else
parent.right = undefined;
};
TreeNode.RED = true;
TreeNode.BLACK = false;
return TreeNode;
}());
export default TreeNode;

View File

@@ -0,0 +1,127 @@
import TreeNode from './TreeNode';
import TreeIterator from './TreeIterator';
import { Container } from "../../ContainerBase/index";
declare abstract class TreeContainer<K, V> extends Container<K | [K, V]> {
protected root: TreeNode<K, V> | undefined;
protected header: TreeNode<K, V>;
protected cmp: (x: K, y: K) => number;
protected constructor(cmp?: (x: K, y: K) => number);
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is greater than or equals to the given key.
* @protected
*/
protected _lowerBound(curNode: TreeNode<K, V> | undefined, key: K): TreeNode<K, V>;
/**
* @param key The given key you want to compare.
* @return An iterator to the first element not less than the given key.
*/
abstract lowerBound(key: K): TreeIterator<K, V>;
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is greater than the given key.
* @protected
*/
protected _upperBound(curNode: TreeNode<K, V> | undefined, key: K): TreeNode<K, V>;
/**
* @param key The given key you want to compare.
* @return An iterator to the first element greater than the given key.
*/
abstract upperBound(key: K): TreeIterator<K, V>;
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is less than or equals to the given key.
* @protected
*/
protected _reverseLowerBound(curNode: TreeNode<K, V> | undefined, key: K): TreeNode<K, V>;
/**
* @param key The given key you want to compare.
* @return An iterator to the first element not greater than the given key.
*/
abstract reverseLowerBound(key: K): TreeIterator<K, V>;
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is less than the given key.
* @protected
*/
protected _reverseUpperBound(curNode: TreeNode<K, V> | undefined, key: K): TreeNode<K, V>;
/**
* @param key The given key you want to compare.
* @return An iterator to the first element less than the given key.
*/
abstract reverseUpperBound(key: K): TreeIterator<K, V>;
/**
* @description Union the other tree to self.
* <br/>
* Waiting for optimization, this is O(mlog(n+m)) algorithm now,
* but we expect it to be O(mlog(n/m+1)).<br/>
* More information =>
* https://en.wikipedia.org/wiki/Red_black_tree
* <br/>
* @param other The other tree container you want to merge.
*/
abstract union(other: TreeContainer<K, V>): void;
/**
* @description Make self balance after erase a node.
* @param curNode The node want to remove.
* @protected
*/
protected eraseNodeSelfBalance(curNode: TreeNode<K, V>): void;
/**
* @description Remove a node.
* @param curNode The node you want to remove.
* @protected
*/
protected eraseNode(curNode: TreeNode<K, V>): void;
/**
* @description InOrder traversal the tree.
* @protected
*/
protected inOrderTraversal: (curNode: TreeNode<K, V> | undefined, callback: (curNode: TreeNode<K, V>) => boolean) => boolean;
/**
* @description Make self balance after insert a node.
* @param curNode The node want to insert.
* @protected
*/
protected insertNodeSelfBalance(curNode: TreeNode<K, V>): void;
/**
* @description Find node which key is equals to the given key.
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @protected
*/
protected findElementNode(curNode: TreeNode<K, V> | undefined, key: K): TreeNode<K, V> | undefined;
/**
* @description Insert a key-value pair or set value by the given key.
* @param key The key want to insert.
* @param value The value want to set.
* @param hint You can give an iterator hint to improve insertion efficiency.
* @protected
*/
protected set(key: K, value?: V, hint?: TreeIterator<K, V>): void;
clear(): void;
/**
* @description Update node's key by iterator.
* @param iter The iterator you want to change.
* @param key The key you want to update.
* @return Boolean about if the modification is successful.
*/
updateKeyByIterator(iter: TreeIterator<K, V>, key: K): boolean;
eraseElementByPos(pos: number): void;
/**
* @description Remove the element of the specified key.
* @param key The key you want to remove.
*/
eraseElementByKey(key: K): void;
eraseElementByIterator(iter: TreeIterator<K, V>): TreeIterator<K, V>;
/**
* @description Get the height of the tree.
* @return Number about the height of the RB-tree.
*/
getHeight(): number;
}
export default TreeContainer;

View File

@@ -0,0 +1,601 @@
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import TreeNode from './TreeNode';
import { Container } from "../../ContainerBase/index";
import { checkWithinAccessParams } from "../../../utils/checkParams";
var TreeContainer = /** @class */ (function (_super) {
__extends(TreeContainer, _super);
function TreeContainer(cmp) {
if (cmp === void 0) { cmp = function (x, y) {
if (x < y)
return -1;
if (x > y)
return 1;
return 0;
}; }
var _this = _super.call(this) || this;
_this.root = undefined;
_this.header = new TreeNode();
/**
* @description InOrder traversal the tree.
* @protected
*/
_this.inOrderTraversal = function (curNode, callback) {
if (curNode === undefined)
return false;
var ifReturn = _this.inOrderTraversal(curNode.left, callback);
if (ifReturn)
return true;
if (callback(curNode))
return true;
return _this.inOrderTraversal(curNode.right, callback);
};
_this.cmp = cmp;
return _this;
}
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is greater than or equals to the given key.
* @protected
*/
TreeContainer.prototype._lowerBound = function (curNode, key) {
var resNode;
while (curNode) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult < 0) {
curNode = curNode.right;
}
else if (cmpResult > 0) {
resNode = curNode;
curNode = curNode.left;
}
else
return curNode;
}
return resNode === undefined ? this.header : resNode;
};
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is greater than the given key.
* @protected
*/
TreeContainer.prototype._upperBound = function (curNode, key) {
var resNode;
while (curNode) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult <= 0) {
curNode = curNode.right;
}
else if (cmpResult > 0) {
resNode = curNode;
curNode = curNode.left;
}
}
return resNode === undefined ? this.header : resNode;
};
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is less than or equals to the given key.
* @protected
*/
TreeContainer.prototype._reverseLowerBound = function (curNode, key) {
var resNode;
while (curNode) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult < 0) {
resNode = curNode;
curNode = curNode.right;
}
else if (cmpResult > 0) {
curNode = curNode.left;
}
else
return curNode;
}
return resNode === undefined ? this.header : resNode;
};
/**
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @return TreeNode which key is less than the given key.
* @protected
*/
TreeContainer.prototype._reverseUpperBound = function (curNode, key) {
var resNode;
while (curNode) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult < 0) {
resNode = curNode;
curNode = curNode.right;
}
else if (cmpResult >= 0) {
curNode = curNode.left;
}
}
return resNode === undefined ? this.header : resNode;
};
/**
* @description Make self balance after erase a node.
* @param curNode The node want to remove.
* @protected
*/
TreeContainer.prototype.eraseNodeSelfBalance = function (curNode) {
while (true) {
var parentNode = curNode.parent;
if (parentNode === this.header)
return;
if (curNode.color === TreeNode.RED) {
curNode.color = TreeNode.BLACK;
return;
}
if (curNode === parentNode.left) {
var brother = parentNode.right;
if (brother.color === TreeNode.RED) {
brother.color = TreeNode.BLACK;
parentNode.color = TreeNode.RED;
if (parentNode === this.root) {
this.root = parentNode.rotateLeft();
}
else
parentNode.rotateLeft();
}
else if (brother.color === TreeNode.BLACK) {
if (brother.right && brother.right.color === TreeNode.RED) {
brother.color = parentNode.color;
parentNode.color = TreeNode.BLACK;
brother.right.color = TreeNode.BLACK;
if (parentNode === this.root) {
this.root = parentNode.rotateLeft();
}
else
parentNode.rotateLeft();
return;
}
else if (brother.left && brother.left.color === TreeNode.RED) {
brother.color = TreeNode.RED;
brother.left.color = TreeNode.BLACK;
brother.rotateRight();
}
else {
brother.color = TreeNode.RED;
curNode = parentNode;
}
}
}
else {
var brother = parentNode.left;
if (brother.color === TreeNode.RED) {
brother.color = TreeNode.BLACK;
parentNode.color = TreeNode.RED;
if (parentNode === this.root) {
this.root = parentNode.rotateRight();
}
else
parentNode.rotateRight();
}
else {
if (brother.left && brother.left.color === TreeNode.RED) {
brother.color = parentNode.color;
parentNode.color = TreeNode.BLACK;
brother.left.color = TreeNode.BLACK;
if (parentNode === this.root) {
this.root = parentNode.rotateRight();
}
else
parentNode.rotateRight();
return;
}
else if (brother.right && brother.right.color === TreeNode.RED) {
brother.color = TreeNode.RED;
brother.right.color = TreeNode.BLACK;
brother.rotateLeft();
}
else {
brother.color = TreeNode.RED;
curNode = parentNode;
}
}
}
}
};
/**
* @description Remove a node.
* @param curNode The node you want to remove.
* @protected
*/
TreeContainer.prototype.eraseNode = function (curNode) {
var _a, _b;
if (this.length === 1) {
this.clear();
return;
}
var swapNode = curNode;
while (swapNode.left || swapNode.right) {
if (swapNode.right) {
swapNode = swapNode.right;
while (swapNode.left)
swapNode = swapNode.left;
}
else if (swapNode.left) {
swapNode = swapNode.left;
}
_a = __read([swapNode.key, curNode.key], 2), curNode.key = _a[0], swapNode.key = _a[1];
_b = __read([swapNode.value, curNode.value], 2), curNode.value = _b[0], swapNode.value = _b[1];
curNode = swapNode;
}
if (this.header.left === swapNode) {
this.header.left = swapNode.parent;
}
else if (this.header.right === swapNode) {
this.header.right = swapNode.parent;
}
this.eraseNodeSelfBalance(swapNode);
swapNode.remove();
this.length -= 1;
this.root.color = TreeNode.BLACK;
};
/**
* @description Make self balance after insert a node.
* @param curNode The node want to insert.
* @protected
*/
TreeContainer.prototype.insertNodeSelfBalance = function (curNode) {
while (true) {
var parentNode = curNode.parent;
if (parentNode.color === TreeNode.BLACK)
return;
var grandParent = parentNode.parent;
if (parentNode === grandParent.left) {
var uncle = grandParent.right;
if (uncle && uncle.color === TreeNode.RED) {
uncle.color = parentNode.color = TreeNode.BLACK;
if (grandParent === this.root)
return;
grandParent.color = TreeNode.RED;
curNode = grandParent;
continue;
}
else if (curNode === parentNode.right) {
curNode.color = TreeNode.BLACK;
if (curNode.left)
curNode.left.parent = parentNode;
if (curNode.right)
curNode.right.parent = grandParent;
parentNode.right = curNode.left;
grandParent.left = curNode.right;
curNode.left = parentNode;
curNode.right = grandParent;
if (grandParent === this.root) {
this.root = curNode;
this.header.parent = curNode;
}
else {
var GP = grandParent.parent;
if (GP.left === grandParent) {
GP.left = curNode;
}
else
GP.right = curNode;
}
curNode.parent = grandParent.parent;
parentNode.parent = curNode;
grandParent.parent = curNode;
}
else {
parentNode.color = TreeNode.BLACK;
if (grandParent === this.root) {
this.root = grandParent.rotateRight();
}
else
grandParent.rotateRight();
}
grandParent.color = TreeNode.RED;
}
else {
var uncle = grandParent.left;
if (uncle && uncle.color === TreeNode.RED) {
uncle.color = parentNode.color = TreeNode.BLACK;
if (grandParent === this.root)
return;
grandParent.color = TreeNode.RED;
curNode = grandParent;
continue;
}
else if (curNode === parentNode.left) {
curNode.color = TreeNode.BLACK;
if (curNode.left)
curNode.left.parent = grandParent;
if (curNode.right)
curNode.right.parent = parentNode;
grandParent.right = curNode.left;
parentNode.left = curNode.right;
curNode.left = grandParent;
curNode.right = parentNode;
if (grandParent === this.root) {
this.root = curNode;
this.header.parent = curNode;
}
else {
var GP = grandParent.parent;
if (GP.left === grandParent) {
GP.left = curNode;
}
else
GP.right = curNode;
}
curNode.parent = grandParent.parent;
parentNode.parent = curNode;
grandParent.parent = curNode;
}
else {
parentNode.color = TreeNode.BLACK;
if (grandParent === this.root) {
this.root = grandParent.rotateLeft();
}
else
grandParent.rotateLeft();
}
grandParent.color = TreeNode.RED;
}
return;
}
};
/**
* @description Find node which key is equals to the given key.
* @param curNode The starting node of the search.
* @param key The key you want to search.
* @protected
*/
TreeContainer.prototype.findElementNode = function (curNode, key) {
while (curNode) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult < 0) {
curNode = curNode.right;
}
else if (cmpResult > 0) {
curNode = curNode.left;
}
else
return curNode;
}
return curNode;
};
/**
* @description Insert a key-value pair or set value by the given key.
* @param key The key want to insert.
* @param value The value want to set.
* @param hint You can give an iterator hint to improve insertion efficiency.
* @protected
*/
TreeContainer.prototype.set = function (key, value, hint) {
if (this.root === undefined) {
this.length += 1;
this.root = new TreeNode(key, value);
this.root.color = TreeNode.BLACK;
this.root.parent = this.header;
this.header.parent = this.root;
this.header.left = this.root;
this.header.right = this.root;
return;
}
var curNode;
var minNode = this.header.left;
var compareToMin = this.cmp(minNode.key, key);
if (compareToMin === 0) {
minNode.value = value;
return;
}
else if (compareToMin > 0) {
minNode.left = new TreeNode(key, value);
minNode.left.parent = minNode;
curNode = minNode.left;
this.header.left = curNode;
}
else {
var maxNode = this.header.right;
var compareToMax = this.cmp(maxNode.key, key);
if (compareToMax === 0) {
maxNode.value = value;
return;
}
else if (compareToMax < 0) {
maxNode.right = new TreeNode(key, value);
maxNode.right.parent = maxNode;
curNode = maxNode.right;
this.header.right = curNode;
}
else {
if (hint !== undefined) {
// @ts-ignore
var iterNode = hint.node;
if (iterNode !== this.header) {
var iterCmpRes = this.cmp(iterNode.key, key);
if (iterCmpRes === 0) {
iterNode.value = value;
return;
}
else if (iterCmpRes > 0) {
var preNode = iterNode.pre();
var preCmpRes = this.cmp(preNode.key, key);
if (preCmpRes === 0) {
preNode.value = value;
return;
}
else if (preCmpRes < 0) {
curNode = new TreeNode(key, value);
if (preNode.right === undefined) {
preNode.right = curNode;
curNode.parent = preNode;
}
else {
iterNode.left = curNode;
curNode.parent = iterNode;
}
}
}
}
}
if (curNode === undefined) {
curNode = this.root;
while (true) {
var cmpResult = this.cmp(curNode.key, key);
if (cmpResult > 0) {
if (curNode.left === undefined) {
curNode.left = new TreeNode(key, value);
curNode.left.parent = curNode;
curNode = curNode.left;
break;
}
curNode = curNode.left;
}
else if (cmpResult < 0) {
if (curNode.right === undefined) {
curNode.right = new TreeNode(key, value);
curNode.right.parent = curNode;
curNode = curNode.right;
break;
}
curNode = curNode.right;
}
else {
curNode.value = value;
return;
}
}
}
}
}
this.length += 1;
this.insertNodeSelfBalance(curNode);
};
TreeContainer.prototype.clear = function () {
this.length = 0;
this.root = undefined;
this.header.parent = undefined;
this.header.left = this.header.right = undefined;
};
/**
* @description Update node's key by iterator.
* @param iter The iterator you want to change.
* @param key The key you want to update.
* @return Boolean about if the modification is successful.
*/
TreeContainer.prototype.updateKeyByIterator = function (iter, key) {
// @ts-ignore
var node = iter.node;
if (node === this.header) {
throw new TypeError('Invalid iterator!');
}
if (this.length === 1) {
node.key = key;
return true;
}
if (node === this.header.left) {
if (this.cmp(node.next().key, key) > 0) {
node.key = key;
return true;
}
return false;
}
if (node === this.header.right) {
if (this.cmp(node.pre().key, key) < 0) {
node.key = key;
return true;
}
return false;
}
var preKey = node.pre().key;
if (this.cmp(preKey, key) >= 0)
return false;
var nextKey = node.next().key;
if (this.cmp(nextKey, key) <= 0)
return false;
node.key = key;
return true;
};
TreeContainer.prototype.eraseElementByPos = function (pos) {
var _this = this;
checkWithinAccessParams(pos, 0, this.length - 1);
var index = 0;
this.inOrderTraversal(this.root, function (curNode) {
if (pos === index) {
_this.eraseNode(curNode);
return true;
}
index += 1;
return false;
});
};
/**
* @description Remove the element of the specified key.
* @param key The key you want to remove.
*/
TreeContainer.prototype.eraseElementByKey = function (key) {
if (!this.length)
return;
var curNode = this.findElementNode(this.root, key);
if (curNode === undefined)
return;
this.eraseNode(curNode);
};
TreeContainer.prototype.eraseElementByIterator = function (iter) {
// @ts-ignore
var node = iter.node;
if (node === this.header) {
throw new RangeError('Invalid iterator');
}
if (node.right === undefined) {
iter = iter.next();
}
this.eraseNode(node);
return iter;
};
/**
* @description Get the height of the tree.
* @return Number about the height of the RB-tree.
*/
TreeContainer.prototype.getHeight = function () {
if (!this.length)
return 0;
var traversal = function (curNode) {
if (!curNode)
return 0;
return Math.max(traversal(curNode.left), traversal(curNode.right)) + 1;
};
return traversal(this.root);
};
return TreeContainer;
}(Container));
export default TreeContainer;

View File

@@ -0,0 +1,38 @@
import { initContainer } from "../ContainerBase/index";
import TreeContainer from './Base/index';
import TreeIterator from './Base/TreeIterator';
export declare class OrderedMapIterator<K, V> extends TreeIterator<K, V> {
get pointer(): [K, V];
copy(): OrderedMapIterator<K, V>;
}
declare class OrderedMap<K, V> extends TreeContainer<K, V> {
constructor(container?: initContainer<[K, V]>, cmp?: (x: K, y: K) => number);
private readonly iterationFunc;
begin(): OrderedMapIterator<K, V>;
end(): OrderedMapIterator<K, V>;
rBegin(): OrderedMapIterator<K, V>;
rEnd(): OrderedMapIterator<K, V>;
front(): [K, V] | undefined;
back(): [K, V] | undefined;
forEach(callback: (element: [K, V], index: number) => void): void;
lowerBound(key: K): OrderedMapIterator<K, V>;
upperBound(key: K): OrderedMapIterator<K, V>;
reverseLowerBound(key: K): OrderedMapIterator<K, V>;
reverseUpperBound(key: K): OrderedMapIterator<K, V>;
/**
* @description Insert a key-value pair or set value by the given key.
* @param key The key want to insert.
* @param value The value want to set.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
setElement(key: K, value: V, hint?: OrderedMapIterator<K, V>): void;
find(key: K): OrderedMapIterator<K, V>;
/**
* @description Get the value of the element of the specified key.
*/
getElementByKey(key: K): V | undefined;
getElementByPos(pos: number): [K, V];
union(other: OrderedMap<K, V>): void;
[Symbol.iterator](): Generator<[K, V], void, undefined>;
}
export default OrderedMap;

View File

@@ -0,0 +1,257 @@
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { ContainerIterator } from "../ContainerBase/index";
import { checkWithinAccessParams } from "../../utils/checkParams";
import TreeContainer from './Base/index';
import TreeIterator from './Base/TreeIterator';
var OrderedMapIterator = /** @class */ (function (_super) {
__extends(OrderedMapIterator, _super);
function OrderedMapIterator() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(OrderedMapIterator.prototype, "pointer", {
get: function () {
var _this = this;
if (this.node === this.header) {
throw new RangeError('OrderedMap iterator access denied');
}
return new Proxy([], {
get: function (_, props) {
if (props === '0')
return _this.node.key;
else if (props === '1')
return _this.node.value;
},
set: function (_, props, newValue) {
if (props !== '1') {
throw new TypeError('props must be 1');
}
_this.node.value = newValue;
return true;
}
});
},
enumerable: false,
configurable: true
});
OrderedMapIterator.prototype.copy = function () {
return new OrderedMapIterator(this.node, this.header, this.iteratorType);
};
return OrderedMapIterator;
}(TreeIterator));
export { OrderedMapIterator };
var OrderedMap = /** @class */ (function (_super) {
__extends(OrderedMap, _super);
function OrderedMap(container, cmp) {
if (container === void 0) { container = []; }
var _this = _super.call(this, cmp) || this;
_this.iterationFunc = function (curNode) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (curNode === undefined)
return [2 /*return*/];
return [5 /*yield**/, __values(this.iterationFunc(curNode.left))];
case 1:
_a.sent();
return [4 /*yield*/, [curNode.key, curNode.value]];
case 2:
_a.sent();
return [5 /*yield**/, __values(this.iterationFunc(curNode.right))];
case 3:
_a.sent();
return [2 /*return*/];
}
});
};
_this.iterationFunc = _this.iterationFunc.bind(_this);
container.forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
return _this.setElement(key, value);
});
return _this;
}
OrderedMap.prototype.begin = function () {
return new OrderedMapIterator(this.header.left || this.header, this.header);
};
OrderedMap.prototype.end = function () {
return new OrderedMapIterator(this.header, this.header);
};
OrderedMap.prototype.rBegin = function () {
return new OrderedMapIterator(this.header.right || this.header, this.header, ContainerIterator.REVERSE);
};
OrderedMap.prototype.rEnd = function () {
return new OrderedMapIterator(this.header, this.header, ContainerIterator.REVERSE);
};
OrderedMap.prototype.front = function () {
if (!this.length)
return undefined;
var minNode = this.header.left;
return [minNode.key, minNode.value];
};
OrderedMap.prototype.back = function () {
if (!this.length)
return undefined;
var maxNode = this.header.right;
return [maxNode.key, maxNode.value];
};
OrderedMap.prototype.forEach = function (callback) {
var e_1, _a;
var index = 0;
try {
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
var pair = _c.value;
callback(pair, index++);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
OrderedMap.prototype.lowerBound = function (key) {
var resNode = this._lowerBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
};
OrderedMap.prototype.upperBound = function (key) {
var resNode = this._upperBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
};
OrderedMap.prototype.reverseLowerBound = function (key) {
var resNode = this._reverseLowerBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
};
OrderedMap.prototype.reverseUpperBound = function (key) {
var resNode = this._reverseUpperBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
};
/**
* @description Insert a key-value pair or set value by the given key.
* @param key The key want to insert.
* @param value The value want to set.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
OrderedMap.prototype.setElement = function (key, value, hint) {
this.set(key, value, hint);
};
OrderedMap.prototype.find = function (key) {
var curNode = this.findElementNode(this.root, key);
if (curNode !== undefined) {
return new OrderedMapIterator(curNode, this.header);
}
return this.end();
};
/**
* @description Get the value of the element of the specified key.
*/
OrderedMap.prototype.getElementByKey = function (key) {
var curNode = this.findElementNode(this.root, key);
return curNode ? curNode.value : undefined;
};
OrderedMap.prototype.getElementByPos = function (pos) {
var e_2, _a;
checkWithinAccessParams(pos, 0, this.length - 1);
var res;
var index = 0;
try {
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
var pair = _c.value;
if (index === pos) {
res = pair;
break;
}
index += 1;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return res;
};
OrderedMap.prototype.union = function (other) {
var _this = this;
other.forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
return _this.setElement(key, value);
});
};
OrderedMap.prototype[Symbol.iterator] = function () {
return this.iterationFunc(this.root);
};
return OrderedMap;
}(TreeContainer));
export default OrderedMap;

View File

@@ -0,0 +1,33 @@
import TreeContainer from './Base/index';
import { initContainer } from "../ContainerBase/index";
import TreeIterator from './Base/TreeIterator';
export declare class OrderedSetIterator<K> extends TreeIterator<K, undefined> {
get pointer(): K;
copy(): OrderedSetIterator<K>;
}
declare class OrderedSet<K> extends TreeContainer<K, undefined> {
constructor(container?: initContainer<K>, cmp?: (x: K, y: K) => number);
private readonly iterationFunc;
begin(): OrderedSetIterator<K>;
end(): OrderedSetIterator<K>;
rBegin(): OrderedSetIterator<K>;
rEnd(): OrderedSetIterator<K>;
front(): K | undefined;
back(): K | undefined;
forEach(callback: (element: K, index: number) => void): void;
getElementByPos(pos: number): K;
/**
* @description Insert element to set.
* @param key The key want to insert.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
insert(key: K, hint?: OrderedSetIterator<K>): void;
find(element: K): OrderedSetIterator<K>;
lowerBound(key: K): OrderedSetIterator<K>;
upperBound(key: K): OrderedSetIterator<K>;
reverseLowerBound(key: K): OrderedSetIterator<K>;
reverseUpperBound(key: K): OrderedSetIterator<K>;
union(other: OrderedSet<K>): void;
[Symbol.iterator](): Generator<K, void, undefined>;
}
export default OrderedSet;

View File

@@ -0,0 +1,205 @@
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import TreeContainer from './Base/index';
import { ContainerIterator } from "../ContainerBase/index";
import { checkWithinAccessParams } from "../../utils/checkParams";
import TreeIterator from './Base/TreeIterator';
var OrderedSetIterator = /** @class */ (function (_super) {
__extends(OrderedSetIterator, _super);
function OrderedSetIterator() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(OrderedSetIterator.prototype, "pointer", {
get: function () {
if (this.node === this.header) {
throw new RangeError('OrderedSet iterator access denied!');
}
return this.node.key;
},
enumerable: false,
configurable: true
});
OrderedSetIterator.prototype.copy = function () {
return new OrderedSetIterator(this.node, this.header, this.iteratorType);
};
return OrderedSetIterator;
}(TreeIterator));
export { OrderedSetIterator };
var OrderedSet = /** @class */ (function (_super) {
__extends(OrderedSet, _super);
function OrderedSet(container, cmp) {
if (container === void 0) { container = []; }
var _this = _super.call(this, cmp) || this;
_this.iterationFunc = function (curNode) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (curNode === undefined)
return [2 /*return*/];
return [5 /*yield**/, __values(this.iterationFunc(curNode.left))];
case 1:
_a.sent();
return [4 /*yield*/, curNode.key];
case 2:
_a.sent();
return [5 /*yield**/, __values(this.iterationFunc(curNode.right))];
case 3:
_a.sent();
return [2 /*return*/];
}
});
};
container.forEach(function (element) { return _this.insert(element); });
_this.iterationFunc = _this.iterationFunc.bind(_this);
return _this;
}
OrderedSet.prototype.begin = function () {
return new OrderedSetIterator(this.header.left || this.header, this.header);
};
OrderedSet.prototype.end = function () {
return new OrderedSetIterator(this.header, this.header);
};
OrderedSet.prototype.rBegin = function () {
return new OrderedSetIterator(this.header.right || this.header, this.header, ContainerIterator.REVERSE);
};
OrderedSet.prototype.rEnd = function () {
return new OrderedSetIterator(this.header, this.header, ContainerIterator.REVERSE);
};
OrderedSet.prototype.front = function () {
return this.header.left ? this.header.left.key : undefined;
};
OrderedSet.prototype.back = function () {
return this.header.right ? this.header.right.key : undefined;
};
OrderedSet.prototype.forEach = function (callback) {
var e_1, _a;
var index = 0;
try {
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
var element = _c.value;
callback(element, index++);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
OrderedSet.prototype.getElementByPos = function (pos) {
var e_2, _a;
checkWithinAccessParams(pos, 0, this.length - 1);
var res;
var index = 0;
try {
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
var element = _c.value;
if (index === pos) {
res = element;
}
index += 1;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return res;
};
/**
* @description Insert element to set.
* @param key The key want to insert.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
OrderedSet.prototype.insert = function (key, hint) {
this.set(key, undefined, hint);
};
OrderedSet.prototype.find = function (element) {
var curNode = this.findElementNode(this.root, element);
if (curNode !== undefined) {
return new OrderedSetIterator(curNode, this.header);
}
return this.end();
};
OrderedSet.prototype.lowerBound = function (key) {
var resNode = this._lowerBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
};
OrderedSet.prototype.upperBound = function (key) {
var resNode = this._upperBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
};
OrderedSet.prototype.reverseLowerBound = function (key) {
var resNode = this._reverseLowerBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
};
OrderedSet.prototype.reverseUpperBound = function (key) {
var resNode = this._reverseUpperBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
};
OrderedSet.prototype.union = function (other) {
var _this = this;
other.forEach(function (element) { return _this.insert(element); });
};
OrderedSet.prototype[Symbol.iterator] = function () {
return this.iterationFunc(this.root);
};
return OrderedSet;
}(TreeContainer));
export default OrderedSet;