first commit

This commit is contained in:
Myk
2025-07-31 23:47:20 +03:00
commit 2186b278a0
5149 changed files with 537218 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import { ContainerIterator } from "../../ContainerBase";
import TreeContainer from "./index";
declare abstract class TreeIterator<K, V> extends ContainerIterator<K | [K, V]> {
abstract readonly container: TreeContainer<K, V>;
/**
* @description Get the sequential index of the iterator in the tree container.<br/>
* <strong>Note:</strong>
* This function only takes effect when the specified tree container `enableIndex = true`.
* @returns The index subscript of the node in the tree.
* @example
* const st = new OrderedSet([1, 2, 3], true);
* console.log(st.begin().next().index); // 1
*/
get index(): number;
pre(): this;
next(): this;
}
export default TreeIterator;

View File

@@ -0,0 +1,98 @@
var __extends = this && this.t || function() {
var extendStatics = function(r, t) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(r, t) {
r.__proto__ = t;
} || function(r, t) {
for (var e in t) if (Object.prototype.hasOwnProperty.call(t, e)) r[e] = t[e];
};
return extendStatics(r, t);
};
return function(r, t) {
if (typeof t !== "function" && t !== null) throw new TypeError("Class extends value " + String(t) + " is not a constructor or null");
extendStatics(r, t);
function __() {
this.constructor = r;
}
r.prototype = t === null ? Object.create(t) : (__.prototype = t.prototype, new __);
};
}();
import { ContainerIterator } from "../../ContainerBase";
import { throwIteratorAccessError } from "../../../utils/throwError";
var TreeIterator = function(r) {
__extends(TreeIterator, r);
function TreeIterator(t, e, i) {
var n = r.call(this, i) || this;
n.o = t;
n.h = e;
if (n.iteratorType === 0) {
n.pre = function() {
if (this.o === this.h.K) {
throwIteratorAccessError();
}
this.o = this.o.L();
return this;
};
n.next = function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
this.o = this.o.m();
return this;
};
} else {
n.pre = function() {
if (this.o === this.h.N) {
throwIteratorAccessError();
}
this.o = this.o.m();
return this;
};
n.next = function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
this.o = this.o.L();
return this;
};
}
return n;
}
Object.defineProperty(TreeIterator.prototype, "index", {
get: function() {
var r = this.o;
var t = this.h.rr;
if (r === this.h) {
if (t) {
return t.tr - 1;
}
return 0;
}
var e = 0;
if (r.K) {
e += r.K.tr;
}
while (r !== t) {
var i = r.rr;
if (r === i.N) {
e += 1;
if (i.K) {
e += i.K.tr;
}
}
r = i;
}
return e;
},
enumerable: false,
configurable: true
});
return TreeIterator;
}(ContainerIterator);
export default TreeIterator;
//# sourceMappingURL=TreeIterator.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,47 @@
export declare const enum TreeNodeColor {
RED = 1,
BLACK = 0
}
export declare class TreeNode<K, V> {
_color: TreeNodeColor;
_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.
* @returns TreeNode about the pre node.
*/
_pre(): TreeNode<K, V>;
/**
* @description Get the next node.
* @returns TreeNode about the next node.
*/
_next(): TreeNode<K, V>;
/**
* @description Rotate left.
* @returns TreeNode about moved to original position after rotation.
*/
_rotateLeft(): TreeNode<K, V>;
/**
* @description Rotate right.
* @returns TreeNode about moved to original position after rotation.
*/
_rotateRight(): TreeNode<K, V>;
}
export declare class TreeNodeEnableIndex<K, V> extends TreeNode<K, V> {
_subTreeSize: number;
/**
* @description Rotate left and do recount.
* @returns TreeNode about moved to original position after rotation.
*/
_rotateLeft(): TreeNodeEnableIndex<K, V>;
/**
* @description Rotate right and do recount.
* @returns TreeNode about moved to original position after rotation.
*/
_rotateRight(): TreeNodeEnableIndex<K, V>;
_recount(): void;
}

View File

@@ -0,0 +1,132 @@
var __extends = this && this.t || function() {
var extendStatics = function(e, n) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(e, n) {
e.__proto__ = n;
} || function(e, n) {
for (var t in n) if (Object.prototype.hasOwnProperty.call(n, t)) e[t] = n[t];
};
return extendStatics(e, n);
};
return function(e, n) {
if (typeof n !== "function" && n !== null) throw new TypeError("Class extends value " + String(n) + " is not a constructor or null");
extendStatics(e, n);
function __() {
this.constructor = e;
}
e.prototype = n === null ? Object.create(n) : (__.prototype = n.prototype, new __);
};
}();
var TreeNode = function() {
function TreeNode(e, n) {
this.ee = 1;
this.u = undefined;
this.p = undefined;
this.K = undefined;
this.N = undefined;
this.rr = undefined;
this.u = e;
this.p = n;
}
TreeNode.prototype.L = function() {
var e = this;
if (e.ee === 1 && e.rr.rr === e) {
e = e.N;
} else if (e.K) {
e = e.K;
while (e.N) {
e = e.N;
}
} else {
var n = e.rr;
while (n.K === e) {
e = n;
n = e.rr;
}
e = n;
}
return e;
};
TreeNode.prototype.m = function() {
var e = this;
if (e.N) {
e = e.N;
while (e.K) {
e = e.K;
}
return e;
} else {
var n = e.rr;
while (n.N === e) {
e = n;
n = e.rr;
}
if (e.N !== n) {
return n;
} else return e;
}
};
TreeNode.prototype.ne = function() {
var e = this.rr;
var n = this.N;
var t = n.K;
if (e.rr === this) e.rr = n; else if (e.K === this) e.K = n; else e.N = n;
n.rr = e;
n.K = this;
this.rr = n;
this.N = t;
if (t) t.rr = this;
return n;
};
TreeNode.prototype.te = function() {
var e = this.rr;
var n = this.K;
var t = n.N;
if (e.rr === this) e.rr = n; else if (e.K === this) e.K = n; else e.N = n;
n.rr = e;
n.N = this;
this.rr = n;
this.K = t;
if (t) t.rr = this;
return n;
};
return TreeNode;
}();
export { TreeNode };
var TreeNodeEnableIndex = function(e) {
__extends(TreeNodeEnableIndex, e);
function TreeNodeEnableIndex() {
var n = e !== null && e.apply(this, arguments) || this;
n.tr = 1;
return n;
}
TreeNodeEnableIndex.prototype.ne = function() {
var n = e.prototype.ne.call(this);
this.ie();
n.ie();
return n;
};
TreeNodeEnableIndex.prototype.te = function() {
var n = e.prototype.te.call(this);
this.ie();
n.ie();
return n;
};
TreeNodeEnableIndex.prototype.ie = function() {
this.tr = 1;
if (this.K) {
this.tr += this.K.tr;
}
if (this.N) {
this.tr += this.N.tr;
}
};
return TreeNodeEnableIndex;
}(TreeNode);
export { TreeNodeEnableIndex };
//# sourceMappingURL=TreeNode.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,58 @@
import type TreeIterator from './TreeIterator';
import { Container } from "../../ContainerBase";
declare abstract class TreeContainer<K, V> extends Container<K | [K, V]> {
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.
* @returns Whether the modification is successful.
* @example
* const st = new orderedSet([1, 2, 5]);
* const iter = st.find(2);
* st.updateKeyByIterator(iter, 3); // then st will become [1, 3, 5]
*/
updateKeyByIterator(iter: TreeIterator<K, V>, key: K): boolean;
eraseElementByPos(pos: number): number;
/**
* @description Remove the element of the specified key.
* @param key - The key you want to remove.
* @returns Whether erase successfully.
*/
eraseElementByKey(key: K): boolean;
eraseElementByIterator(iter: TreeIterator<K, V>): TreeIterator<K, V>;
forEach(callback: (element: K | [K, V], index: number, tree: TreeContainer<K, V>) => void): void;
getElementByPos(pos: number): K | [K, V];
/**
* @description Get the height of the tree.
* @returns Number about the height of the RB-tree.
*/
getHeight(): number;
/**
* @param key - The given key you want to compare.
* @returns 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.
* @param other - The other tree container you want to merge.
* @returns The size of the tree after union.
*/
abstract union(other: TreeContainer<K, V>): number;
/**
* @param key - The given key you want to compare.
* @returns An iterator to the first element not greater than the given key.
*/
abstract reverseLowerBound(key: K): TreeIterator<K, V>;
/**
* @param key - The given key you want to compare.
* @returns An iterator to the first element not less than the given key.
*/
abstract lowerBound(key: K): TreeIterator<K, V>;
/**
* @param key - The given key you want to compare.
* @returns An iterator to the first element greater than the given key.
*/
abstract upperBound(key: K): TreeIterator<K, V>;
}
export default TreeContainer;

View File

@@ -0,0 +1,600 @@
var __extends = this && this.t || function() {
var extendStatics = function(e, r) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(e, r) {
e.__proto__ = r;
} || function(e, r) {
for (var i in r) if (Object.prototype.hasOwnProperty.call(r, i)) e[i] = r[i];
};
return extendStatics(e, r);
};
return function(e, r) {
if (typeof r !== "function" && r !== null) throw new TypeError("Class extends value " + String(r) + " is not a constructor or null");
extendStatics(e, r);
function __() {
this.constructor = e;
}
e.prototype = r === null ? Object.create(r) : (__.prototype = r.prototype, new __);
};
}();
var __read = this && this.q || function(e, r) {
var i = typeof Symbol === "function" && e[Symbol.iterator];
if (!i) return e;
var t = i.call(e), n, s = [], f;
try {
while ((r === void 0 || r-- > 0) && !(n = t.next()).done) s.push(n.value);
} catch (e) {
f = {
error: e
};
} finally {
try {
if (n && !n.done && (i = t["return"])) i.call(t);
} finally {
if (f) throw f.error;
}
}
return s;
};
var __values = this && this.V || function(e) {
var r = typeof Symbol === "function" && Symbol.iterator, i = r && e[r], t = 0;
if (i) return i.call(e);
if (e && typeof e.length === "number") return {
next: function() {
if (e && t >= e.length) e = void 0;
return {
value: e && e[t++],
done: !e
};
}
};
throw new TypeError(r ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { TreeNode, TreeNodeEnableIndex } from "./TreeNode";
import { Container } from "../../ContainerBase";
import { throwIteratorAccessError } from "../../../utils/throwError";
var TreeContainer = function(e) {
__extends(TreeContainer, e);
function TreeContainer(r, i) {
if (r === void 0) {
r = function(e, r) {
if (e < r) return -1;
if (e > r) return 1;
return 0;
};
}
if (i === void 0) {
i = false;
}
var t = e.call(this) || this;
t.W = undefined;
t.$ = r;
if (i) {
t.re = TreeNodeEnableIndex;
t.v = function(e, r, i) {
var t = this.se(e, r, i);
if (t) {
var n = t.rr;
while (n !== this.h) {
n.tr += 1;
n = n.rr;
}
var s = this.fe(t);
if (s) {
var f = s, h = f.parentNode, u = f.grandParent, a = f.curNode;
h.ie();
u.ie();
a.ie();
}
}
return this.M;
};
t.G = function(e) {
var r = this.he(e);
while (r !== this.h) {
r.tr -= 1;
r = r.rr;
}
};
} else {
t.re = TreeNode;
t.v = function(e, r, i) {
var t = this.se(e, r, i);
if (t) this.fe(t);
return this.M;
};
t.G = t.he;
}
t.h = new t.re;
return t;
}
TreeContainer.prototype.U = function(e, r) {
var i = this.h;
while (e) {
var t = this.$(e.u, r);
if (t < 0) {
e = e.N;
} else if (t > 0) {
i = e;
e = e.K;
} else return e;
}
return i;
};
TreeContainer.prototype.X = function(e, r) {
var i = this.h;
while (e) {
var t = this.$(e.u, r);
if (t <= 0) {
e = e.N;
} else {
i = e;
e = e.K;
}
}
return i;
};
TreeContainer.prototype.Y = function(e, r) {
var i = this.h;
while (e) {
var t = this.$(e.u, r);
if (t < 0) {
i = e;
e = e.N;
} else if (t > 0) {
e = e.K;
} else return e;
}
return i;
};
TreeContainer.prototype.Z = function(e, r) {
var i = this.h;
while (e) {
var t = this.$(e.u, r);
if (t < 0) {
i = e;
e = e.N;
} else {
e = e.K;
}
}
return i;
};
TreeContainer.prototype.ue = function(e) {
while (true) {
var r = e.rr;
if (r === this.h) return;
if (e.ee === 1) {
e.ee = 0;
return;
}
if (e === r.K) {
var i = r.N;
if (i.ee === 1) {
i.ee = 0;
r.ee = 1;
if (r === this.W) {
this.W = r.ne();
} else r.ne();
} else {
if (i.N && i.N.ee === 1) {
i.ee = r.ee;
r.ee = 0;
i.N.ee = 0;
if (r === this.W) {
this.W = r.ne();
} else r.ne();
return;
} else if (i.K && i.K.ee === 1) {
i.ee = 1;
i.K.ee = 0;
i.te();
} else {
i.ee = 1;
e = r;
}
}
} else {
var i = r.K;
if (i.ee === 1) {
i.ee = 0;
r.ee = 1;
if (r === this.W) {
this.W = r.te();
} else r.te();
} else {
if (i.K && i.K.ee === 1) {
i.ee = r.ee;
r.ee = 0;
i.K.ee = 0;
if (r === this.W) {
this.W = r.te();
} else r.te();
return;
} else if (i.N && i.N.ee === 1) {
i.ee = 1;
i.N.ee = 0;
i.ne();
} else {
i.ee = 1;
e = r;
}
}
}
}
};
TreeContainer.prototype.he = function(e) {
var r, i;
if (this.M === 1) {
this.clear();
return this.h;
}
var t = e;
while (t.K || t.N) {
if (t.N) {
t = t.N;
while (t.K) t = t.K;
} else {
t = t.K;
}
r = __read([ t.u, e.u ], 2), e.u = r[0], t.u = r[1];
i = __read([ t.p, e.p ], 2), e.p = i[0], t.p = i[1];
e = t;
}
if (this.h.K === t) {
this.h.K = t.rr;
} else if (this.h.N === t) {
this.h.N = t.rr;
}
this.ue(t);
var n = t.rr;
if (t === n.K) {
n.K = undefined;
} else n.N = undefined;
this.M -= 1;
this.W.ee = 0;
return n;
};
TreeContainer.prototype.ae = function(e, r) {
if (e === undefined) return false;
var i = this.ae(e.K, r);
if (i) return true;
if (r(e)) return true;
return this.ae(e.N, r);
};
TreeContainer.prototype.fe = function(e) {
while (true) {
var r = e.rr;
if (r.ee === 0) return;
var i = r.rr;
if (r === i.K) {
var t = i.N;
if (t && t.ee === 1) {
t.ee = r.ee = 0;
if (i === this.W) return;
i.ee = 1;
e = i;
continue;
} else if (e === r.N) {
e.ee = 0;
if (e.K) e.K.rr = r;
if (e.N) e.N.rr = i;
r.N = e.K;
i.K = e.N;
e.K = r;
e.N = i;
if (i === this.W) {
this.W = e;
this.h.rr = e;
} else {
var n = i.rr;
if (n.K === i) {
n.K = e;
} else n.N = e;
}
e.rr = i.rr;
r.rr = e;
i.rr = e;
i.ee = 1;
return {
parentNode: r,
grandParent: i,
curNode: e
};
} else {
r.ee = 0;
if (i === this.W) {
this.W = i.te();
} else i.te();
i.ee = 1;
}
} else {
var t = i.K;
if (t && t.ee === 1) {
t.ee = r.ee = 0;
if (i === this.W) return;
i.ee = 1;
e = i;
continue;
} else if (e === r.K) {
e.ee = 0;
if (e.K) e.K.rr = i;
if (e.N) e.N.rr = r;
i.N = e.K;
r.K = e.N;
e.K = i;
e.N = r;
if (i === this.W) {
this.W = e;
this.h.rr = e;
} else {
var n = i.rr;
if (n.K === i) {
n.K = e;
} else n.N = e;
}
e.rr = i.rr;
r.rr = e;
i.rr = e;
i.ee = 1;
return {
parentNode: r,
grandParent: i,
curNode: e
};
} else {
r.ee = 0;
if (i === this.W) {
this.W = i.ne();
} else i.ne();
i.ee = 1;
}
}
return;
}
};
TreeContainer.prototype.se = function(e, r, i) {
if (this.W === undefined) {
this.M += 1;
this.W = new this.re(e, r);
this.W.ee = 0;
this.W.rr = this.h;
this.h.rr = this.W;
this.h.K = this.W;
this.h.N = this.W;
return;
}
var t;
var n = this.h.K;
var s = this.$(n.u, e);
if (s === 0) {
n.p = r;
return;
} else if (s > 0) {
n.K = new this.re(e, r);
n.K.rr = n;
t = n.K;
this.h.K = t;
} else {
var f = this.h.N;
var h = this.$(f.u, e);
if (h === 0) {
f.p = r;
return;
} else if (h < 0) {
f.N = new this.re(e, r);
f.N.rr = f;
t = f.N;
this.h.N = t;
} else {
if (i !== undefined) {
var u = i.o;
if (u !== this.h) {
var a = this.$(u.u, e);
if (a === 0) {
u.p = r;
return;
} else if (a > 0) {
var o = u.L();
var l = this.$(o.u, e);
if (l === 0) {
o.p = r;
return;
} else if (l < 0) {
t = new this.re(e, r);
if (o.N === undefined) {
o.N = t;
t.rr = o;
} else {
u.K = t;
t.rr = u;
}
}
}
}
}
if (t === undefined) {
t = this.W;
while (true) {
var v = this.$(t.u, e);
if (v > 0) {
if (t.K === undefined) {
t.K = new this.re(e, r);
t.K.rr = t;
t = t.K;
break;
}
t = t.K;
} else if (v < 0) {
if (t.N === undefined) {
t.N = new this.re(e, r);
t.N.rr = t;
t = t.N;
break;
}
t = t.N;
} else {
t.p = r;
return;
}
}
}
}
}
this.M += 1;
return t;
};
TreeContainer.prototype.g = function(e, r) {
while (e) {
var i = this.$(e.u, r);
if (i < 0) {
e = e.N;
} else if (i > 0) {
e = e.K;
} else return e;
}
return e || this.h;
};
TreeContainer.prototype.clear = function() {
this.M = 0;
this.W = undefined;
this.h.rr = undefined;
this.h.K = this.h.N = undefined;
};
TreeContainer.prototype.updateKeyByIterator = function(e, r) {
var i = e.o;
if (i === this.h) {
throwIteratorAccessError();
}
if (this.M === 1) {
i.u = r;
return true;
}
if (i === this.h.K) {
if (this.$(i.m().u, r) > 0) {
i.u = r;
return true;
}
return false;
}
if (i === this.h.N) {
if (this.$(i.L().u, r) < 0) {
i.u = r;
return true;
}
return false;
}
var t = i.L().u;
if (this.$(t, r) >= 0) return false;
var n = i.m().u;
if (this.$(n, r) <= 0) return false;
i.u = r;
return true;
};
TreeContainer.prototype.eraseElementByPos = function(e) {
if (e < 0 || e > this.M - 1) {
throw new RangeError;
}
var r = 0;
var i = this;
this.ae(this.W, (function(t) {
if (e === r) {
i.G(t);
return true;
}
r += 1;
return false;
}));
return this.M;
};
TreeContainer.prototype.eraseElementByKey = function(e) {
if (this.M === 0) return false;
var r = this.g(this.W, e);
if (r === this.h) return false;
this.G(r);
return true;
};
TreeContainer.prototype.eraseElementByIterator = function(e) {
var r = e.o;
if (r === this.h) {
throwIteratorAccessError();
}
var i = r.N === undefined;
var t = e.iteratorType === 0;
if (t) {
if (i) e.next();
} else {
if (!i || r.K === undefined) e.next();
}
this.G(r);
return e;
};
TreeContainer.prototype.forEach = function(e) {
var r, i;
var t = 0;
try {
for (var n = __values(this), s = n.next(); !s.done; s = n.next()) {
var f = s.value;
e(f, t++, this);
}
} catch (e) {
r = {
error: e
};
} finally {
try {
if (s && !s.done && (i = n.return)) i.call(n);
} finally {
if (r) throw r.error;
}
}
};
TreeContainer.prototype.getElementByPos = function(e) {
var r, i;
if (e < 0 || e > this.M - 1) {
throw new RangeError;
}
var t;
var n = 0;
try {
for (var s = __values(this), f = s.next(); !f.done; f = s.next()) {
var h = f.value;
if (n === e) {
t = h;
break;
}
n += 1;
}
} catch (e) {
r = {
error: e
};
} finally {
try {
if (f && !f.done && (i = s.return)) i.call(s);
} finally {
if (r) throw r.error;
}
}
return t;
};
TreeContainer.prototype.getHeight = function() {
if (this.M === 0) return 0;
var traversal = function(e) {
if (!e) return 0;
return Math.max(traversal(e.K), traversal(e.N)) + 1;
};
return traversal(this.W);
};
return TreeContainer;
}(Container);
export default TreeContainer;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,62 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { TreeNode } from './Base/TreeNode';
import { initContainer, IteratorType } from "../ContainerBase";
declare class OrderedMapIterator<K, V> extends TreeIterator<K, V> {
container: OrderedMap<K, V>;
constructor(node: TreeNode<K, V>, header: TreeNode<K, V>, container: OrderedMap<K, V>, iteratorType?: IteratorType);
get pointer(): [K, V];
copy(): OrderedMapIterator<K, V>;
equals(iter: OrderedMapIterator<K, V>): boolean;
}
export type { OrderedMapIterator };
declare class OrderedMap<K, V> extends TreeContainer<K, V> {
/**
* @param container - The initialization container.
* @param cmp - The compare function.
* @param enableIndex - Whether to enable iterator indexing function.
* @example
* new OrderedMap();
* new OrderedMap([[0, 1], [2, 1]]);
* new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y);
* new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y, true);
*/
constructor(container?: initContainer<[K, V]>, cmp?: (x: K, y: K) => number, enableIndex?: boolean);
begin(): OrderedMapIterator<K, V>;
end(): OrderedMapIterator<K, V>;
rBegin(): OrderedMapIterator<K, V>;
rEnd(): OrderedMapIterator<K, V>;
front(): [K, V] | undefined;
back(): [K, V] | undefined;
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.
* @return The size of container after setting.
* @example
* const mp = new OrderedMap([[2, 0], [4, 0], [5, 0]]);
* const iter = mp.begin();
* mp.setElement(1, 0);
* mp.setElement(3, 0, iter); // give a hint will be faster.
*/
setElement(key: K, value: V, hint?: OrderedMapIterator<K, V>): number;
find(key: K): OrderedMapIterator<K, V>;
/**
* @description Get the value of the element of the specified key.
* @param key - The specified key you want to get.
* @example
* const val = container.getElementByKey(1);
*/
getElementByKey(key: K): V | undefined;
union(other: OrderedMap<K, V>): number;
[Symbol.iterator](): Generator<[K, V], void, unknown>;
eraseElementByIterator(iter: OrderedMapIterator<K, V>): OrderedMapIterator<K, V>;
forEach(callback: (element: [K, V], index: number, map: OrderedMap<K, V>) => void): void;
getElementByPos(pos: number): [K, V];
}
export default OrderedMap;

View File

@@ -0,0 +1,265 @@
var __extends = this && this.t || function() {
var extendStatics = function(r, e) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(r, e) {
r.__proto__ = e;
} || function(r, e) {
for (var t in e) if (Object.prototype.hasOwnProperty.call(e, t)) r[t] = e[t];
};
return extendStatics(r, e);
};
return function(r, e) {
if (typeof e !== "function" && e !== null) throw new TypeError("Class extends value " + String(e) + " is not a constructor or null");
extendStatics(r, e);
function __() {
this.constructor = r;
}
r.prototype = e === null ? Object.create(e) : (__.prototype = e.prototype, new __);
};
}();
var __generator = this && this.i || function(r, e) {
var t = {
label: 0,
sent: function() {
if (o[0] & 1) throw o[1];
return o[1];
},
trys: [],
ops: []
}, n, i, o, a;
return a = {
next: verb(0),
throw: verb(1),
return: verb(2)
}, typeof Symbol === "function" && (a[Symbol.iterator] = function() {
return this;
}), a;
function verb(r) {
return function(e) {
return step([ r, e ]);
};
}
function step(a) {
if (n) throw new TypeError("Generator is already executing.");
while (t) try {
if (n = 1, i && (o = a[0] & 2 ? i["return"] : a[0] ? i["throw"] || ((o = i["return"]) && o.call(i),
0) : i.next) && !(o = o.call(i, a[1])).done) return o;
if (i = 0, o) a = [ a[0] & 2, o.value ];
switch (a[0]) {
case 0:
case 1:
o = a;
break;
case 4:
t.label++;
return {
value: a[1],
done: false
};
case 5:
t.label++;
i = a[1];
a = [ 0 ];
continue;
case 7:
a = t.ops.pop();
t.trys.pop();
continue;
default:
if (!(o = t.trys, o = o.length > 0 && o[o.length - 1]) && (a[0] === 6 || a[0] === 2)) {
t = 0;
continue;
}
if (a[0] === 3 && (!o || a[1] > o[0] && a[1] < o[3])) {
t.label = a[1];
break;
}
if (a[0] === 6 && t.label < o[1]) {
t.label = o[1];
o = a;
break;
}
if (o && t.label < o[2]) {
t.label = o[2];
t.ops.push(a);
break;
}
if (o[2]) t.ops.pop();
t.trys.pop();
continue;
}
a = e.call(r, t);
} catch (r) {
a = [ 6, r ];
i = 0;
} finally {
n = o = 0;
}
if (a[0] & 5) throw a[1];
return {
value: a[0] ? a[1] : void 0,
done: true
};
}
};
var __values = this && this.V || function(r) {
var e = typeof Symbol === "function" && Symbol.iterator, t = e && r[e], n = 0;
if (t) return t.call(r);
if (r && typeof r.length === "number") return {
next: function() {
if (r && n >= r.length) r = void 0;
return {
value: r && r[n++],
done: !r
};
}
};
throw new TypeError(e ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import TreeContainer from "./Base";
import TreeIterator from "./Base/TreeIterator";
import { throwIteratorAccessError } from "../../utils/throwError";
var OrderedMapIterator = function(r) {
__extends(OrderedMapIterator, r);
function OrderedMapIterator(e, t, n, i) {
var o = r.call(this, e, t, i) || this;
o.container = n;
return o;
}
Object.defineProperty(OrderedMapIterator.prototype, "pointer", {
get: function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
var r = this;
return new Proxy([], {
get: function(e, t) {
if (t === "0") return r.o.u; else if (t === "1") return r.o.p;
},
set: function(e, t, n) {
if (t !== "1") {
throw new TypeError("props must be 1");
}
r.o.p = n;
return true;
}
});
},
enumerable: false,
configurable: true
});
OrderedMapIterator.prototype.copy = function() {
return new OrderedMapIterator(this.o, this.h, this.container, this.iteratorType);
};
return OrderedMapIterator;
}(TreeIterator);
var OrderedMap = function(r) {
__extends(OrderedMap, r);
function OrderedMap(e, t, n) {
if (e === void 0) {
e = [];
}
var i = r.call(this, t, n) || this;
var o = i;
e.forEach((function(r) {
o.setElement(r[0], r[1]);
}));
return i;
}
OrderedMap.prototype.P = function(r) {
return __generator(this, (function(e) {
switch (e.label) {
case 0:
if (r === undefined) return [ 2 ];
return [ 5, __values(this.P(r.K)) ];
case 1:
e.sent();
return [ 4, [ r.u, r.p ] ];
case 2:
e.sent();
return [ 5, __values(this.P(r.N)) ];
case 3:
e.sent();
return [ 2 ];
}
}));
};
OrderedMap.prototype.begin = function() {
return new OrderedMapIterator(this.h.K || this.h, this.h, this);
};
OrderedMap.prototype.end = function() {
return new OrderedMapIterator(this.h, this.h, this);
};
OrderedMap.prototype.rBegin = function() {
return new OrderedMapIterator(this.h.N || this.h, this.h, this, 1);
};
OrderedMap.prototype.rEnd = function() {
return new OrderedMapIterator(this.h, this.h, this, 1);
};
OrderedMap.prototype.front = function() {
if (this.M === 0) return;
var r = this.h.K;
return [ r.u, r.p ];
};
OrderedMap.prototype.back = function() {
if (this.M === 0) return;
var r = this.h.N;
return [ r.u, r.p ];
};
OrderedMap.prototype.lowerBound = function(r) {
var e = this.U(this.W, r);
return new OrderedMapIterator(e, this.h, this);
};
OrderedMap.prototype.upperBound = function(r) {
var e = this.X(this.W, r);
return new OrderedMapIterator(e, this.h, this);
};
OrderedMap.prototype.reverseLowerBound = function(r) {
var e = this.Y(this.W, r);
return new OrderedMapIterator(e, this.h, this);
};
OrderedMap.prototype.reverseUpperBound = function(r) {
var e = this.Z(this.W, r);
return new OrderedMapIterator(e, this.h, this);
};
OrderedMap.prototype.setElement = function(r, e, t) {
return this.v(r, e, t);
};
OrderedMap.prototype.find = function(r) {
var e = this.g(this.W, r);
return new OrderedMapIterator(e, this.h, this);
};
OrderedMap.prototype.getElementByKey = function(r) {
var e = this.g(this.W, r);
return e.p;
};
OrderedMap.prototype.union = function(r) {
var e = this;
r.forEach((function(r) {
e.setElement(r[0], r[1]);
}));
return this.M;
};
OrderedMap.prototype[Symbol.iterator] = function() {
return this.P(this.W);
};
return OrderedMap;
}(TreeContainer);
export default OrderedMap;
//# sourceMappingURL=OrderedMap.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,54 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { TreeNode } from './Base/TreeNode';
import { initContainer, IteratorType } from "../ContainerBase";
declare class OrderedSetIterator<K> extends TreeIterator<K, undefined> {
container: OrderedSet<K>;
constructor(node: TreeNode<K, undefined>, header: TreeNode<K, undefined>, container: OrderedSet<K>, iteratorType?: IteratorType);
get pointer(): NonNullable<K>;
copy(): OrderedSetIterator<K>;
equals(iter: OrderedSetIterator<K>): boolean;
}
export type { OrderedSetIterator };
declare class OrderedSet<K> extends TreeContainer<K, undefined> {
/**
* @param container - The initialization container.
* @param cmp - The compare function.
* @param enableIndex - Whether to enable iterator indexing function.
* @example
* new OrderedSet();
* new OrderedSet([0, 1, 2]);
* new OrderedSet([0, 1, 2], (x, y) => x - y);
* new OrderedSet([0, 1, 2], (x, y) => x - y, true);
*/
constructor(container?: initContainer<K>, cmp?: (x: K, y: K) => number, enableIndex?: boolean);
begin(): OrderedSetIterator<K>;
end(): OrderedSetIterator<K>;
rBegin(): OrderedSetIterator<K>;
rEnd(): OrderedSetIterator<K>;
front(): K | undefined;
back(): K | undefined;
/**
* @description Insert element to set.
* @param key - The key want to insert.
* @param hint - You can give an iterator hint to improve insertion efficiency.
* @return The size of container after setting.
* @example
* const st = new OrderedSet([2, 4, 5]);
* const iter = st.begin();
* st.insert(1);
* st.insert(3, iter); // give a hint will be faster.
*/
insert(key: K, hint?: OrderedSetIterator<K>): number;
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>): number;
[Symbol.iterator](): Generator<K, void, unknown>;
eraseElementByIterator(iter: OrderedSetIterator<K>): OrderedSetIterator<K>;
forEach(callback: (element: K, index: number, tree: OrderedSet<K>) => void): void;
getElementByPos(pos: number): K;
}
export default OrderedSet;

View File

@@ -0,0 +1,245 @@
var __extends = this && this.t || function() {
var extendStatics = function(e, t) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(e, t) {
e.__proto__ = t;
} || function(e, t) {
for (var r in t) if (Object.prototype.hasOwnProperty.call(t, r)) e[r] = t[r];
};
return extendStatics(e, t);
};
return function(e, t) {
if (typeof t !== "function" && t !== null) throw new TypeError("Class extends value " + String(t) + " is not a constructor or null");
extendStatics(e, t);
function __() {
this.constructor = e;
}
e.prototype = t === null ? Object.create(t) : (__.prototype = t.prototype, new __);
};
}();
var __generator = this && this.i || function(e, t) {
var r = {
label: 0,
sent: function() {
if (o[0] & 1) throw o[1];
return o[1];
},
trys: [],
ops: []
}, n, i, o, u;
return u = {
next: verb(0),
throw: verb(1),
return: verb(2)
}, typeof Symbol === "function" && (u[Symbol.iterator] = function() {
return this;
}), u;
function verb(e) {
return function(t) {
return step([ e, t ]);
};
}
function step(u) {
if (n) throw new TypeError("Generator is already executing.");
while (r) try {
if (n = 1, i && (o = u[0] & 2 ? i["return"] : u[0] ? i["throw"] || ((o = i["return"]) && o.call(i),
0) : i.next) && !(o = o.call(i, u[1])).done) return o;
if (i = 0, o) u = [ u[0] & 2, o.value ];
switch (u[0]) {
case 0:
case 1:
o = u;
break;
case 4:
r.label++;
return {
value: u[1],
done: false
};
case 5:
r.label++;
i = u[1];
u = [ 0 ];
continue;
case 7:
u = r.ops.pop();
r.trys.pop();
continue;
default:
if (!(o = r.trys, o = o.length > 0 && o[o.length - 1]) && (u[0] === 6 || u[0] === 2)) {
r = 0;
continue;
}
if (u[0] === 3 && (!o || u[1] > o[0] && u[1] < o[3])) {
r.label = u[1];
break;
}
if (u[0] === 6 && r.label < o[1]) {
r.label = o[1];
o = u;
break;
}
if (o && r.label < o[2]) {
r.label = o[2];
r.ops.push(u);
break;
}
if (o[2]) r.ops.pop();
r.trys.pop();
continue;
}
u = t.call(e, r);
} catch (e) {
u = [ 6, e ];
i = 0;
} finally {
n = o = 0;
}
if (u[0] & 5) throw u[1];
return {
value: u[0] ? u[1] : void 0,
done: true
};
}
};
var __values = this && this.V || function(e) {
var t = typeof Symbol === "function" && Symbol.iterator, r = t && e[t], n = 0;
if (r) return r.call(e);
if (e && typeof e.length === "number") return {
next: function() {
if (e && n >= e.length) e = void 0;
return {
value: e && e[n++],
done: !e
};
}
};
throw new TypeError(t ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import TreeContainer from "./Base";
import TreeIterator from "./Base/TreeIterator";
import { throwIteratorAccessError } from "../../utils/throwError";
var OrderedSetIterator = function(e) {
__extends(OrderedSetIterator, e);
function OrderedSetIterator(t, r, n, i) {
var o = e.call(this, t, r, i) || this;
o.container = n;
return o;
}
Object.defineProperty(OrderedSetIterator.prototype, "pointer", {
get: function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
return this.o.u;
},
enumerable: false,
configurable: true
});
OrderedSetIterator.prototype.copy = function() {
return new OrderedSetIterator(this.o, this.h, this.container, this.iteratorType);
};
return OrderedSetIterator;
}(TreeIterator);
var OrderedSet = function(e) {
__extends(OrderedSet, e);
function OrderedSet(t, r, n) {
if (t === void 0) {
t = [];
}
var i = e.call(this, r, n) || this;
var o = i;
t.forEach((function(e) {
o.insert(e);
}));
return i;
}
OrderedSet.prototype.P = function(e) {
return __generator(this, (function(t) {
switch (t.label) {
case 0:
if (e === undefined) return [ 2 ];
return [ 5, __values(this.P(e.K)) ];
case 1:
t.sent();
return [ 4, e.u ];
case 2:
t.sent();
return [ 5, __values(this.P(e.N)) ];
case 3:
t.sent();
return [ 2 ];
}
}));
};
OrderedSet.prototype.begin = function() {
return new OrderedSetIterator(this.h.K || this.h, this.h, this);
};
OrderedSet.prototype.end = function() {
return new OrderedSetIterator(this.h, this.h, this);
};
OrderedSet.prototype.rBegin = function() {
return new OrderedSetIterator(this.h.N || this.h, this.h, this, 1);
};
OrderedSet.prototype.rEnd = function() {
return new OrderedSetIterator(this.h, this.h, this, 1);
};
OrderedSet.prototype.front = function() {
return this.h.K ? this.h.K.u : undefined;
};
OrderedSet.prototype.back = function() {
return this.h.N ? this.h.N.u : undefined;
};
OrderedSet.prototype.insert = function(e, t) {
return this.v(e, undefined, t);
};
OrderedSet.prototype.find = function(e) {
var t = this.g(this.W, e);
return new OrderedSetIterator(t, this.h, this);
};
OrderedSet.prototype.lowerBound = function(e) {
var t = this.U(this.W, e);
return new OrderedSetIterator(t, this.h, this);
};
OrderedSet.prototype.upperBound = function(e) {
var t = this.X(this.W, e);
return new OrderedSetIterator(t, this.h, this);
};
OrderedSet.prototype.reverseLowerBound = function(e) {
var t = this.Y(this.W, e);
return new OrderedSetIterator(t, this.h, this);
};
OrderedSet.prototype.reverseUpperBound = function(e) {
var t = this.Z(this.W, e);
return new OrderedSetIterator(t, this.h, this);
};
OrderedSet.prototype.union = function(e) {
var t = this;
e.forEach((function(e) {
t.insert(e);
}));
return this.M;
};
OrderedSet.prototype[Symbol.iterator] = function() {
return this.P(this.W);
};
return OrderedSet;
}(TreeContainer);
export default OrderedSet;
//# sourceMappingURL=OrderedSet.js.map

File diff suppressed because one or more lines are too long