create project

This commit is contained in:
ismailsosic
2022-12-27 12:05:56 +01:00
parent 2a33a2d3de
commit cd2143287c
16035 changed files with 2489703 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { ContainerIterator } from "../../ContainerBase";
declare abstract class TreeIterator<K, V> extends ContainerIterator<K | [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.er) {
throwIteratorAccessError();
}
this.o = this.o.W();
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.tr) {
throwIteratorAccessError();
}
this.o = this.o.m();
return this;
};
n.next = function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
this.o = this.o.W();
return this;
};
}
return n;
}
Object.defineProperty(TreeIterator.prototype, "index", {
get: function() {
var r = this.o;
var t = this.h.hr;
if (r === this.h) {
if (t) {
return t.cr - 1;
}
return 0;
}
var e = 0;
if (r.er) {
e += r.er.cr;
}
while (r !== t) {
var i = r.hr;
if (r === i.tr) {
e += 1;
if (i.er) {
e += i.er.cr;
}
}
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 @@
export {};

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.p = undefined;
this.H = undefined;
this.er = undefined;
this.tr = undefined;
this.hr = undefined;
this.p = e;
this.H = n;
}
TreeNode.prototype.W = function() {
var e = this;
if (e.ee === 1 && e.hr.hr === e) {
e = e.tr;
} else if (e.er) {
e = e.er;
while (e.tr) {
e = e.tr;
}
} else {
var n = e.hr;
while (n.er === e) {
e = n;
n = e.hr;
}
e = n;
}
return e;
};
TreeNode.prototype.m = function() {
var e = this;
if (e.tr) {
e = e.tr;
while (e.er) {
e = e.er;
}
return e;
} else {
var n = e.hr;
while (n.tr === e) {
e = n;
n = e.hr;
}
if (e.tr !== n) {
return n;
} else return e;
}
};
TreeNode.prototype.ne = function() {
var e = this.hr;
var n = this.tr;
var t = n.er;
if (e.hr === this) e.hr = n; else if (e.er === this) e.er = n; else e.tr = n;
n.hr = e;
n.er = this;
this.hr = n;
this.tr = t;
if (t) t.hr = this;
return n;
};
TreeNode.prototype.te = function() {
var e = this.hr;
var n = this.er;
var t = n.tr;
if (e.hr === this) e.hr = n; else if (e.er === this) e.er = n; else e.tr = n;
n.hr = e;
n.tr = this;
this.hr = n;
this.er = t;
if (t) t.hr = 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.cr = 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.cr = 1;
if (this.er) {
this.cr += this.er.cr;
}
if (this.tr) {
this.cr += this.tr.cr;
}
};
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.P || 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.Z || 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.ir = undefined;
t.j = r;
if (i) {
t.re = TreeNodeEnableIndex;
t.v = function(e, r, i) {
var t = this.se(e, r, i);
if (t) {
var n = t.hr;
while (n !== this.h) {
n.cr += 1;
n = n.hr;
}
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.i;
};
t.X = function(e) {
var r = this.he(e);
while (r !== this.h) {
r.cr -= 1;
r = r.hr;
}
};
} else {
t.re = TreeNode;
t.v = function(e, r, i) {
var t = this.se(e, r, i);
if (t) this.fe(t);
return this.i;
};
t.X = t.he;
}
t.h = new t.re;
return t;
}
TreeContainer.prototype.nr = function(e, r) {
var i = this.h;
while (e) {
var t = this.j(e.p, r);
if (t < 0) {
e = e.tr;
} else if (t > 0) {
i = e;
e = e.er;
} else return e;
}
return i;
};
TreeContainer.prototype.ar = function(e, r) {
var i = this.h;
while (e) {
var t = this.j(e.p, r);
if (t <= 0) {
e = e.tr;
} else {
i = e;
e = e.er;
}
}
return i;
};
TreeContainer.prototype.ur = function(e, r) {
var i = this.h;
while (e) {
var t = this.j(e.p, r);
if (t < 0) {
i = e;
e = e.tr;
} else if (t > 0) {
e = e.er;
} else return e;
}
return i;
};
TreeContainer.prototype.sr = function(e, r) {
var i = this.h;
while (e) {
var t = this.j(e.p, r);
if (t < 0) {
i = e;
e = e.tr;
} else {
e = e.er;
}
}
return i;
};
TreeContainer.prototype.ue = function(e) {
while (true) {
var r = e.hr;
if (r === this.h) return;
if (e.ee === 1) {
e.ee = 0;
return;
}
if (e === r.er) {
var i = r.tr;
if (i.ee === 1) {
i.ee = 0;
r.ee = 1;
if (r === this.ir) {
this.ir = r.ne();
} else r.ne();
} else {
if (i.tr && i.tr.ee === 1) {
i.ee = r.ee;
r.ee = 0;
i.tr.ee = 0;
if (r === this.ir) {
this.ir = r.ne();
} else r.ne();
return;
} else if (i.er && i.er.ee === 1) {
i.ee = 1;
i.er.ee = 0;
i.te();
} else {
i.ee = 1;
e = r;
}
}
} else {
var i = r.er;
if (i.ee === 1) {
i.ee = 0;
r.ee = 1;
if (r === this.ir) {
this.ir = r.te();
} else r.te();
} else {
if (i.er && i.er.ee === 1) {
i.ee = r.ee;
r.ee = 0;
i.er.ee = 0;
if (r === this.ir) {
this.ir = r.te();
} else r.te();
return;
} else if (i.tr && i.tr.ee === 1) {
i.ee = 1;
i.tr.ee = 0;
i.ne();
} else {
i.ee = 1;
e = r;
}
}
}
}
};
TreeContainer.prototype.he = function(e) {
var r, i;
if (this.i === 1) {
this.clear();
return this.h;
}
var t = e;
while (t.er || t.tr) {
if (t.tr) {
t = t.tr;
while (t.er) t = t.er;
} else {
t = t.er;
}
r = __read([ t.p, e.p ], 2), e.p = r[0], t.p = r[1];
i = __read([ t.H, e.H ], 2), e.H = i[0], t.H = i[1];
e = t;
}
if (this.h.er === t) {
this.h.er = t.hr;
} else if (this.h.tr === t) {
this.h.tr = t.hr;
}
this.ue(t);
var n = t.hr;
if (t === n.er) {
n.er = undefined;
} else n.tr = undefined;
this.i -= 1;
this.ir.ee = 0;
return n;
};
TreeContainer.prototype.ae = function(e, r) {
if (e === undefined) return false;
var i = this.ae(e.er, r);
if (i) return true;
if (r(e)) return true;
return this.ae(e.tr, r);
};
TreeContainer.prototype.fe = function(e) {
while (true) {
var r = e.hr;
if (r.ee === 0) return;
var i = r.hr;
if (r === i.er) {
var t = i.tr;
if (t && t.ee === 1) {
t.ee = r.ee = 0;
if (i === this.ir) return;
i.ee = 1;
e = i;
continue;
} else if (e === r.tr) {
e.ee = 0;
if (e.er) e.er.hr = r;
if (e.tr) e.tr.hr = i;
r.tr = e.er;
i.er = e.tr;
e.er = r;
e.tr = i;
if (i === this.ir) {
this.ir = e;
this.h.hr = e;
} else {
var n = i.hr;
if (n.er === i) {
n.er = e;
} else n.tr = e;
}
e.hr = i.hr;
r.hr = e;
i.hr = e;
i.ee = 1;
return {
parentNode: r,
grandParent: i,
curNode: e
};
} else {
r.ee = 0;
if (i === this.ir) {
this.ir = i.te();
} else i.te();
i.ee = 1;
}
} else {
var t = i.er;
if (t && t.ee === 1) {
t.ee = r.ee = 0;
if (i === this.ir) return;
i.ee = 1;
e = i;
continue;
} else if (e === r.er) {
e.ee = 0;
if (e.er) e.er.hr = i;
if (e.tr) e.tr.hr = r;
i.tr = e.er;
r.er = e.tr;
e.er = i;
e.tr = r;
if (i === this.ir) {
this.ir = e;
this.h.hr = e;
} else {
var n = i.hr;
if (n.er === i) {
n.er = e;
} else n.tr = e;
}
e.hr = i.hr;
r.hr = e;
i.hr = e;
i.ee = 1;
return {
parentNode: r,
grandParent: i,
curNode: e
};
} else {
r.ee = 0;
if (i === this.ir) {
this.ir = i.ne();
} else i.ne();
i.ee = 1;
}
}
return;
}
};
TreeContainer.prototype.se = function(e, r, i) {
if (this.ir === undefined) {
this.i += 1;
this.ir = new this.re(e, r);
this.ir.ee = 0;
this.ir.hr = this.h;
this.h.hr = this.ir;
this.h.er = this.ir;
this.h.tr = this.ir;
return;
}
var t;
var n = this.h.er;
var s = this.j(n.p, e);
if (s === 0) {
n.H = r;
return;
} else if (s > 0) {
n.er = new this.re(e, r);
n.er.hr = n;
t = n.er;
this.h.er = t;
} else {
var f = this.h.tr;
var h = this.j(f.p, e);
if (h === 0) {
f.H = r;
return;
} else if (h < 0) {
f.tr = new this.re(e, r);
f.tr.hr = f;
t = f.tr;
this.h.tr = t;
} else {
if (i !== undefined) {
var u = i.o;
if (u !== this.h) {
var a = this.j(u.p, e);
if (a === 0) {
u.H = r;
return;
} else if (a > 0) {
var o = u.W();
var l = this.j(o.p, e);
if (l === 0) {
o.H = r;
return;
} else if (l < 0) {
t = new this.re(e, r);
if (o.tr === undefined) {
o.tr = t;
t.hr = o;
} else {
u.er = t;
t.hr = u;
}
}
}
}
}
if (t === undefined) {
t = this.ir;
while (true) {
var v = this.j(t.p, e);
if (v > 0) {
if (t.er === undefined) {
t.er = new this.re(e, r);
t.er.hr = t;
t = t.er;
break;
}
t = t.er;
} else if (v < 0) {
if (t.tr === undefined) {
t.tr = new this.re(e, r);
t.tr.hr = t;
t = t.tr;
break;
}
t = t.tr;
} else {
t.H = r;
return;
}
}
}
}
}
this.i += 1;
return t;
};
TreeContainer.prototype.g = function(e, r) {
while (e) {
var i = this.j(e.p, r);
if (i < 0) {
e = e.tr;
} else if (i > 0) {
e = e.er;
} else return e;
}
return e || this.h;
};
TreeContainer.prototype.clear = function() {
this.i = 0;
this.ir = undefined;
this.h.hr = undefined;
this.h.er = this.h.tr = undefined;
};
TreeContainer.prototype.updateKeyByIterator = function(e, r) {
var i = e.o;
if (i === this.h) {
throwIteratorAccessError();
}
if (this.i === 1) {
i.p = r;
return true;
}
if (i === this.h.er) {
if (this.j(i.m().p, r) > 0) {
i.p = r;
return true;
}
return false;
}
if (i === this.h.tr) {
if (this.j(i.W().p, r) < 0) {
i.p = r;
return true;
}
return false;
}
var t = i.W().p;
if (this.j(t, r) >= 0) return false;
var n = i.m().p;
if (this.j(n, r) <= 0) return false;
i.p = r;
return true;
};
TreeContainer.prototype.eraseElementByPos = function(e) {
if (e < 0 || e > this.i - 1) {
throw new RangeError;
}
var r = 0;
var i = this;
this.ae(this.ir, (function(t) {
if (e === r) {
i.X(t);
return true;
}
r += 1;
return false;
}));
return this.i;
};
TreeContainer.prototype.eraseElementByKey = function(e) {
if (this.i === 0) return false;
var r = this.g(this.ir, e);
if (r === this.h) return false;
this.X(r);
return true;
};
TreeContainer.prototype.eraseElementByIterator = function(e) {
var r = e.o;
if (r === this.h) {
throwIteratorAccessError();
}
var i = r.tr === undefined;
var t = e.iteratorType === 0;
if (t) {
if (i) e.next();
} else {
if (!i || r.er === undefined) e.next();
}
this.X(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.i - 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.i === 0) return 0;
var traversal = function(e) {
if (!e) return 0;
return Math.max(traversal(e.er), traversal(e.tr)) + 1;
};
return traversal(this.ir);
};
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,59 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { initContainer } from "../ContainerBase";
declare class OrderedMapIterator<K, V> extends TreeIterator<K, V> {
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,263 @@
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.u || 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.Z || 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() {
return r !== null && r.apply(this, arguments) || this;
}
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.p; else if (t === "1") return r.o.H;
},
set: function(e, t, n) {
if (t !== "1") {
throw new TypeError("props must be 1");
}
r.o.H = n;
return true;
}
});
},
enumerable: false,
configurable: true
});
OrderedMapIterator.prototype.copy = function() {
return new OrderedMapIterator(this.o, this.h, 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.rr = function(r) {
return __generator(this, (function(e) {
switch (e.label) {
case 0:
if (r === undefined) return [ 2 ];
return [ 5, __values(this.rr(r.er)) ];
case 1:
e.sent();
return [ 4, [ r.p, r.H ] ];
case 2:
e.sent();
return [ 5, __values(this.rr(r.tr)) ];
case 3:
e.sent();
return [ 2 ];
}
}));
};
OrderedMap.prototype.begin = function() {
return new OrderedMapIterator(this.h.er || this.h, this.h);
};
OrderedMap.prototype.end = function() {
return new OrderedMapIterator(this.h, this.h);
};
OrderedMap.prototype.rBegin = function() {
return new OrderedMapIterator(this.h.tr || this.h, this.h, 1);
};
OrderedMap.prototype.rEnd = function() {
return new OrderedMapIterator(this.h, this.h, 1);
};
OrderedMap.prototype.front = function() {
if (this.i === 0) return;
var r = this.h.er;
return [ r.p, r.H ];
};
OrderedMap.prototype.back = function() {
if (this.i === 0) return;
var r = this.h.tr;
return [ r.p, r.H ];
};
OrderedMap.prototype.lowerBound = function(r) {
var e = this.nr(this.ir, r);
return new OrderedMapIterator(e, this.h);
};
OrderedMap.prototype.upperBound = function(r) {
var e = this.ar(this.ir, r);
return new OrderedMapIterator(e, this.h);
};
OrderedMap.prototype.reverseLowerBound = function(r) {
var e = this.ur(this.ir, r);
return new OrderedMapIterator(e, this.h);
};
OrderedMap.prototype.reverseUpperBound = function(r) {
var e = this.sr(this.ir, r);
return new OrderedMapIterator(e, this.h);
};
OrderedMap.prototype.setElement = function(r, e, t) {
return this.v(r, e, t);
};
OrderedMap.prototype.find = function(r) {
var e = this.g(this.ir, r);
return new OrderedMapIterator(e, this.h);
};
OrderedMap.prototype.getElementByKey = function(r) {
var e = this.g(this.ir, r);
return e.H;
};
OrderedMap.prototype.union = function(r) {
var e = this;
r.forEach((function(r) {
e.setElement(r[0], r[1]);
}));
return this.i;
};
OrderedMap.prototype[Symbol.iterator] = function() {
return this.rr(this.ir);
};
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,51 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { initContainer } from "../ContainerBase";
declare class OrderedSetIterator<K> extends TreeIterator<K, undefined> {
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,243 @@
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 t in r) if (Object.prototype.hasOwnProperty.call(r, t)) e[t] = r[t];
};
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 __generator = this && this.u || function(e, r) {
var t = {
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(r) {
return step([ e, r ]);
};
}
function step(u) {
if (n) throw new TypeError("Generator is already executing.");
while (t) 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:
t.label++;
return {
value: u[1],
done: false
};
case 5:
t.label++;
i = u[1];
u = [ 0 ];
continue;
case 7:
u = t.ops.pop();
t.trys.pop();
continue;
default:
if (!(o = t.trys, o = o.length > 0 && o[o.length - 1]) && (u[0] === 6 || u[0] === 2)) {
t = 0;
continue;
}
if (u[0] === 3 && (!o || u[1] > o[0] && u[1] < o[3])) {
t.label = u[1];
break;
}
if (u[0] === 6 && t.label < o[1]) {
t.label = o[1];
o = u;
break;
}
if (o && t.label < o[2]) {
t.label = o[2];
t.ops.push(u);
break;
}
if (o[2]) t.ops.pop();
t.trys.pop();
continue;
}
u = r.call(e, t);
} 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.Z || function(e) {
var r = typeof Symbol === "function" && Symbol.iterator, t = r && e[r], n = 0;
if (t) return t.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(r ? "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() {
return e !== null && e.apply(this, arguments) || this;
}
Object.defineProperty(OrderedSetIterator.prototype, "pointer", {
get: function() {
if (this.o === this.h) {
throwIteratorAccessError();
}
return this.o.p;
},
enumerable: false,
configurable: true
});
OrderedSetIterator.prototype.copy = function() {
return new OrderedSetIterator(this.o, this.h, this.iteratorType);
};
return OrderedSetIterator;
}(TreeIterator);
var OrderedSet = function(e) {
__extends(OrderedSet, e);
function OrderedSet(r, t, n) {
if (r === void 0) {
r = [];
}
var i = e.call(this, t, n) || this;
var o = i;
r.forEach((function(e) {
o.insert(e);
}));
return i;
}
OrderedSet.prototype.rr = function(e) {
return __generator(this, (function(r) {
switch (r.label) {
case 0:
if (e === undefined) return [ 2 ];
return [ 5, __values(this.rr(e.er)) ];
case 1:
r.sent();
return [ 4, e.p ];
case 2:
r.sent();
return [ 5, __values(this.rr(e.tr)) ];
case 3:
r.sent();
return [ 2 ];
}
}));
};
OrderedSet.prototype.begin = function() {
return new OrderedSetIterator(this.h.er || this.h, this.h);
};
OrderedSet.prototype.end = function() {
return new OrderedSetIterator(this.h, this.h);
};
OrderedSet.prototype.rBegin = function() {
return new OrderedSetIterator(this.h.tr || this.h, this.h, 1);
};
OrderedSet.prototype.rEnd = function() {
return new OrderedSetIterator(this.h, this.h, 1);
};
OrderedSet.prototype.front = function() {
return this.h.er ? this.h.er.p : undefined;
};
OrderedSet.prototype.back = function() {
return this.h.tr ? this.h.tr.p : undefined;
};
OrderedSet.prototype.insert = function(e, r) {
return this.v(e, undefined, r);
};
OrderedSet.prototype.find = function(e) {
var r = this.g(this.ir, e);
return new OrderedSetIterator(r, this.h);
};
OrderedSet.prototype.lowerBound = function(e) {
var r = this.nr(this.ir, e);
return new OrderedSetIterator(r, this.h);
};
OrderedSet.prototype.upperBound = function(e) {
var r = this.ar(this.ir, e);
return new OrderedSetIterator(r, this.h);
};
OrderedSet.prototype.reverseLowerBound = function(e) {
var r = this.ur(this.ir, e);
return new OrderedSetIterator(r, this.h);
};
OrderedSet.prototype.reverseUpperBound = function(e) {
var r = this.sr(this.ir, e);
return new OrderedSetIterator(r, this.h);
};
OrderedSet.prototype.union = function(e) {
var r = this;
e.forEach((function(e) {
r.insert(e);
}));
return this.i;
};
OrderedSet.prototype[Symbol.iterator] = function() {
return this.rr(this.ir);
};
return OrderedSet;
}(TreeContainer);
export default OrderedSet;
//# sourceMappingURL=OrderedSet.js.map

File diff suppressed because one or more lines are too long