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,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../../ContainerBase/index");
class TreeIterator extends index_1.ContainerIterator {
constructor(node, header, iteratorType) {
super(iteratorType);
this.node = node;
this.header = header;
if (this.iteratorType === index_1.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;
};
}
}
equals(obj) {
return this.node === obj.node;
}
}
exports.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,122 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class TreeNode {
constructor(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.
*/
pre() {
let 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 {
let 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.
*/
next() {
let nextNode = this;
if (nextNode.right) {
nextNode = nextNode.right;
while (nextNode.left) {
nextNode = nextNode.left;
}
}
else {
let 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.
*/
rotateLeft() {
const PP = this.parent;
const V = this.right;
const 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.
*/
rotateRight() {
const PP = this.parent;
const F = this.left;
const 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.
*/
remove() {
const parent = this.parent;
if (this === parent.left) {
parent.left = undefined;
}
else
parent.right = undefined;
}
}
TreeNode.RED = true;
TreeNode.BLACK = false;
exports.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,569 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const TreeNode_1 = __importDefault(require("./TreeNode"));
const index_1 = require("../../ContainerBase/index");
const checkParams_1 = require("../../../utils/checkParams");
class TreeContainer extends index_1.Container {
constructor(cmp = (x, y) => {
if (x < y)
return -1;
if (x > y)
return 1;
return 0;
}) {
super();
this.root = undefined;
this.header = new TreeNode_1.default();
/**
* @description InOrder traversal the tree.
* @protected
*/
this.inOrderTraversal = (curNode, callback) => {
if (curNode === undefined)
return false;
const ifReturn = this.inOrderTraversal(curNode.left, callback);
if (ifReturn)
return true;
if (callback(curNode))
return true;
return this.inOrderTraversal(curNode.right, callback);
};
this.cmp = cmp;
}
/**
* @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
*/
_lowerBound(curNode, key) {
let resNode;
while (curNode) {
const 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
*/
_upperBound(curNode, key) {
let resNode;
while (curNode) {
const 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
*/
_reverseLowerBound(curNode, key) {
let resNode;
while (curNode) {
const 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
*/
_reverseUpperBound(curNode, key) {
let resNode;
while (curNode) {
const 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
*/
eraseNodeSelfBalance(curNode) {
while (true) {
const parentNode = curNode.parent;
if (parentNode === this.header)
return;
if (curNode.color === TreeNode_1.default.RED) {
curNode.color = TreeNode_1.default.BLACK;
return;
}
if (curNode === parentNode.left) {
const brother = parentNode.right;
if (brother.color === TreeNode_1.default.RED) {
brother.color = TreeNode_1.default.BLACK;
parentNode.color = TreeNode_1.default.RED;
if (parentNode === this.root) {
this.root = parentNode.rotateLeft();
}
else
parentNode.rotateLeft();
}
else if (brother.color === TreeNode_1.default.BLACK) {
if (brother.right && brother.right.color === TreeNode_1.default.RED) {
brother.color = parentNode.color;
parentNode.color = TreeNode_1.default.BLACK;
brother.right.color = TreeNode_1.default.BLACK;
if (parentNode === this.root) {
this.root = parentNode.rotateLeft();
}
else
parentNode.rotateLeft();
return;
}
else if (brother.left && brother.left.color === TreeNode_1.default.RED) {
brother.color = TreeNode_1.default.RED;
brother.left.color = TreeNode_1.default.BLACK;
brother.rotateRight();
}
else {
brother.color = TreeNode_1.default.RED;
curNode = parentNode;
}
}
}
else {
const brother = parentNode.left;
if (brother.color === TreeNode_1.default.RED) {
brother.color = TreeNode_1.default.BLACK;
parentNode.color = TreeNode_1.default.RED;
if (parentNode === this.root) {
this.root = parentNode.rotateRight();
}
else
parentNode.rotateRight();
}
else {
if (brother.left && brother.left.color === TreeNode_1.default.RED) {
brother.color = parentNode.color;
parentNode.color = TreeNode_1.default.BLACK;
brother.left.color = TreeNode_1.default.BLACK;
if (parentNode === this.root) {
this.root = parentNode.rotateRight();
}
else
parentNode.rotateRight();
return;
}
else if (brother.right && brother.right.color === TreeNode_1.default.RED) {
brother.color = TreeNode_1.default.RED;
brother.right.color = TreeNode_1.default.BLACK;
brother.rotateLeft();
}
else {
brother.color = TreeNode_1.default.RED;
curNode = parentNode;
}
}
}
}
}
/**
* @description Remove a node.
* @param curNode The node you want to remove.
* @protected
*/
eraseNode(curNode) {
if (this.length === 1) {
this.clear();
return;
}
let 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;
}
[curNode.key, swapNode.key] = [swapNode.key, curNode.key];
[curNode.value, swapNode.value] = [swapNode.value, curNode.value];
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_1.default.BLACK;
}
/**
* @description Make self balance after insert a node.
* @param curNode The node want to insert.
* @protected
*/
insertNodeSelfBalance(curNode) {
while (true) {
const parentNode = curNode.parent;
if (parentNode.color === TreeNode_1.default.BLACK)
return;
const grandParent = parentNode.parent;
if (parentNode === grandParent.left) {
const uncle = grandParent.right;
if (uncle && uncle.color === TreeNode_1.default.RED) {
uncle.color = parentNode.color = TreeNode_1.default.BLACK;
if (grandParent === this.root)
return;
grandParent.color = TreeNode_1.default.RED;
curNode = grandParent;
continue;
}
else if (curNode === parentNode.right) {
curNode.color = TreeNode_1.default.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 {
const 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_1.default.BLACK;
if (grandParent === this.root) {
this.root = grandParent.rotateRight();
}
else
grandParent.rotateRight();
}
grandParent.color = TreeNode_1.default.RED;
}
else {
const uncle = grandParent.left;
if (uncle && uncle.color === TreeNode_1.default.RED) {
uncle.color = parentNode.color = TreeNode_1.default.BLACK;
if (grandParent === this.root)
return;
grandParent.color = TreeNode_1.default.RED;
curNode = grandParent;
continue;
}
else if (curNode === parentNode.left) {
curNode.color = TreeNode_1.default.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 {
const 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_1.default.BLACK;
if (grandParent === this.root) {
this.root = grandParent.rotateLeft();
}
else
grandParent.rotateLeft();
}
grandParent.color = TreeNode_1.default.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
*/
findElementNode(curNode, key) {
while (curNode) {
const 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
*/
set(key, value, hint) {
if (this.root === undefined) {
this.length += 1;
this.root = new TreeNode_1.default(key, value);
this.root.color = TreeNode_1.default.BLACK;
this.root.parent = this.header;
this.header.parent = this.root;
this.header.left = this.root;
this.header.right = this.root;
return;
}
let curNode;
const minNode = this.header.left;
const compareToMin = this.cmp(minNode.key, key);
if (compareToMin === 0) {
minNode.value = value;
return;
}
else if (compareToMin > 0) {
minNode.left = new TreeNode_1.default(key, value);
minNode.left.parent = minNode;
curNode = minNode.left;
this.header.left = curNode;
}
else {
const maxNode = this.header.right;
const compareToMax = this.cmp(maxNode.key, key);
if (compareToMax === 0) {
maxNode.value = value;
return;
}
else if (compareToMax < 0) {
maxNode.right = new TreeNode_1.default(key, value);
maxNode.right.parent = maxNode;
curNode = maxNode.right;
this.header.right = curNode;
}
else {
if (hint !== undefined) {
// @ts-ignore
const iterNode = hint.node;
if (iterNode !== this.header) {
const iterCmpRes = this.cmp(iterNode.key, key);
if (iterCmpRes === 0) {
iterNode.value = value;
return;
}
else if (iterCmpRes > 0) {
const preNode = iterNode.pre();
const preCmpRes = this.cmp(preNode.key, key);
if (preCmpRes === 0) {
preNode.value = value;
return;
}
else if (preCmpRes < 0) {
curNode = new TreeNode_1.default(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) {
const cmpResult = this.cmp(curNode.key, key);
if (cmpResult > 0) {
if (curNode.left === undefined) {
curNode.left = new TreeNode_1.default(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_1.default(key, value);
curNode.right.parent = curNode;
curNode = curNode.right;
break;
}
curNode = curNode.right;
}
else {
curNode.value = value;
return;
}
}
}
}
}
this.length += 1;
this.insertNodeSelfBalance(curNode);
}
clear() {
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.
*/
updateKeyByIterator(iter, key) {
// @ts-ignore
const 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;
}
const preKey = node.pre().key;
if (this.cmp(preKey, key) >= 0)
return false;
const nextKey = node.next().key;
if (this.cmp(nextKey, key) <= 0)
return false;
node.key = key;
return true;
}
eraseElementByPos(pos) {
(0, checkParams_1.checkWithinAccessParams)(pos, 0, this.length - 1);
let index = 0;
this.inOrderTraversal(this.root, 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.
*/
eraseElementByKey(key) {
if (!this.length)
return;
const curNode = this.findElementNode(this.root, key);
if (curNode === undefined)
return;
this.eraseNode(curNode);
}
eraseElementByIterator(iter) {
// @ts-ignore
const 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.
*/
getHeight() {
if (!this.length)
return 0;
const traversal = (curNode) => {
if (!curNode)
return 0;
return Math.max(traversal(curNode.left), traversal(curNode.right)) + 1;
};
return traversal(this.root);
}
}
exports.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,138 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderedMapIterator = void 0;
const index_1 = require("../ContainerBase/index");
const checkParams_1 = require("../../utils/checkParams");
const index_2 = __importDefault(require("./Base/index"));
const TreeIterator_1 = __importDefault(require("./Base/TreeIterator"));
class OrderedMapIterator extends TreeIterator_1.default {
get pointer() {
if (this.node === this.header) {
throw new RangeError('OrderedMap iterator access denied');
}
return new Proxy([], {
get: (_, props) => {
if (props === '0')
return this.node.key;
else if (props === '1')
return this.node.value;
},
set: (_, props, newValue) => {
if (props !== '1') {
throw new TypeError('props must be 1');
}
this.node.value = newValue;
return true;
}
});
}
copy() {
return new OrderedMapIterator(this.node, this.header, this.iteratorType);
}
}
exports.OrderedMapIterator = OrderedMapIterator;
class OrderedMap extends index_2.default {
constructor(container = [], cmp) {
super(cmp);
this.iterationFunc = function* (curNode) {
if (curNode === undefined)
return;
yield* this.iterationFunc(curNode.left);
yield [curNode.key, curNode.value];
yield* this.iterationFunc(curNode.right);
};
this.iterationFunc = this.iterationFunc.bind(this);
container.forEach(([key, value]) => this.setElement(key, value));
}
begin() {
return new OrderedMapIterator(this.header.left || this.header, this.header);
}
end() {
return new OrderedMapIterator(this.header, this.header);
}
rBegin() {
return new OrderedMapIterator(this.header.right || this.header, this.header, index_1.ContainerIterator.REVERSE);
}
rEnd() {
return new OrderedMapIterator(this.header, this.header, index_1.ContainerIterator.REVERSE);
}
front() {
if (!this.length)
return undefined;
const minNode = this.header.left;
return [minNode.key, minNode.value];
}
back() {
if (!this.length)
return undefined;
const maxNode = this.header.right;
return [maxNode.key, maxNode.value];
}
forEach(callback) {
let index = 0;
for (const pair of this)
callback(pair, index++);
}
lowerBound(key) {
const resNode = this._lowerBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
}
upperBound(key) {
const resNode = this._upperBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
}
reverseLowerBound(key) {
const resNode = this._reverseLowerBound(this.root, key);
return new OrderedMapIterator(resNode, this.header);
}
reverseUpperBound(key) {
const 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.
*/
setElement(key, value, hint) {
this.set(key, value, hint);
}
find(key) {
const 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.
*/
getElementByKey(key) {
const curNode = this.findElementNode(this.root, key);
return curNode ? curNode.value : undefined;
}
getElementByPos(pos) {
(0, checkParams_1.checkWithinAccessParams)(pos, 0, this.length - 1);
let res;
let index = 0;
for (const pair of this) {
if (index === pos) {
res = pair;
break;
}
index += 1;
}
return res;
}
union(other) {
other.forEach(([key, value]) => this.setElement(key, value));
}
[Symbol.iterator]() {
return this.iterationFunc(this.root);
}
}
exports.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,109 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderedSetIterator = void 0;
const index_1 = __importDefault(require("./Base/index"));
const index_2 = require("../ContainerBase/index");
const checkParams_1 = require("../../utils/checkParams");
const TreeIterator_1 = __importDefault(require("./Base/TreeIterator"));
class OrderedSetIterator extends TreeIterator_1.default {
get pointer() {
if (this.node === this.header) {
throw new RangeError('OrderedSet iterator access denied!');
}
return this.node.key;
}
copy() {
return new OrderedSetIterator(this.node, this.header, this.iteratorType);
}
}
exports.OrderedSetIterator = OrderedSetIterator;
class OrderedSet extends index_1.default {
constructor(container = [], cmp) {
super(cmp);
this.iterationFunc = function* (curNode) {
if (curNode === undefined)
return;
yield* this.iterationFunc(curNode.left);
yield curNode.key;
yield* this.iterationFunc(curNode.right);
};
container.forEach((element) => this.insert(element));
this.iterationFunc = this.iterationFunc.bind(this);
}
begin() {
return new OrderedSetIterator(this.header.left || this.header, this.header);
}
end() {
return new OrderedSetIterator(this.header, this.header);
}
rBegin() {
return new OrderedSetIterator(this.header.right || this.header, this.header, index_2.ContainerIterator.REVERSE);
}
rEnd() {
return new OrderedSetIterator(this.header, this.header, index_2.ContainerIterator.REVERSE);
}
front() {
return this.header.left ? this.header.left.key : undefined;
}
back() {
return this.header.right ? this.header.right.key : undefined;
}
forEach(callback) {
let index = 0;
for (const element of this)
callback(element, index++);
}
getElementByPos(pos) {
(0, checkParams_1.checkWithinAccessParams)(pos, 0, this.length - 1);
let res;
let index = 0;
for (const element of this) {
if (index === pos) {
res = element;
}
index += 1;
}
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.
*/
insert(key, hint) {
this.set(key, undefined, hint);
}
find(element) {
const curNode = this.findElementNode(this.root, element);
if (curNode !== undefined) {
return new OrderedSetIterator(curNode, this.header);
}
return this.end();
}
lowerBound(key) {
const resNode = this._lowerBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
}
upperBound(key) {
const resNode = this._upperBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
}
reverseLowerBound(key) {
const resNode = this._reverseLowerBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
}
reverseUpperBound(key) {
const resNode = this._reverseUpperBound(this.root, key);
return new OrderedSetIterator(resNode, this.header);
}
union(other) {
other.forEach((element) => this.insert(element));
}
[Symbol.iterator]() {
return this.iterationFunc(this.root);
}
}
exports.default = OrderedSet;