initial commit

This commit is contained in:
2025-09-01 22:12:29 +02:00
parent b1873f9c1d
commit 02a54f61c0
5598 changed files with 903558 additions and 0 deletions

View File

@@ -0,0 +1,402 @@
/**
* @description The iterator type including `NORMAL` and `REVERSE`.
*/
declare const enum IteratorType {
NORMAL = 0,
REVERSE = 1
}
declare abstract class ContainerIterator<T> {
/**
* @description The container pointed to by the iterator.
*/
abstract readonly container: Container<T>;
/**
* @description Iterator's type.
* @example
* console.log(container.end().iteratorType === IteratorType.NORMAL); // true
*/
readonly iteratorType: IteratorType;
/**
* @param iter - The other iterator you want to compare.
* @returns Whether this equals to obj.
* @example
* container.find(1).equals(container.end());
*/
equals(iter: ContainerIterator<T>): boolean;
/**
* @description Pointers to element.
* @returns The value of the pointer's element.
* @example
* const val = container.begin().pointer;
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue - The new value you want to set.
* @example
* (<LinkList<number>>container).begin().pointer = 1;
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [0, 1]
* const pre = iter.pre();
* console.log(pre === iter); // true
* console.log(pre.equals(iter)); // true
* console.log(pre.pointer, iter.pointer); // 0, 0
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.next();
* console.log(next === iter); // true
* console.log(next.equals(iter)); // true
* console.log(next.pointer, iter.pointer); // 2, 2
*/
abstract next(): this;
/**
* @description Get a copy of itself.
* @returns The copy of self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.copy().next();
* console.log(next === iter); // false
* console.log(next.equals(iter)); // false
* console.log(next.pointer, iter.pointer); // 2, 1
*/
abstract copy(): ContainerIterator<T>;
abstract isAccessible(): boolean;
}
declare abstract class Base {
/**
* @returns The size of the container.
* @example
* const container = new Vector([1, 2]);
* console.log(container.length); // 2
*/
get length(): number;
/**
* @returns The size of the container.
* @example
* const container = new Vector([1, 2]);
* console.log(container.size()); // 2
*/
size(): number;
/**
* @returns Whether the container is empty.
* @example
* container.clear();
* console.log(container.empty()); // true
*/
empty(): boolean;
/**
* @description Clear the container.
* @example
* container.clear();
* console.log(container.empty()); // true
*/
abstract clear(): void;
}
declare abstract class Container<T> extends Base {
/**
* @returns Iterator pointing to the beginning element.
* @example
* const begin = container.begin();
* const end = container.end();
* for (const it = begin; !it.equals(end); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract begin(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the super end like c++.
* @example
* const begin = container.begin();
* const end = container.end();
* for (const it = begin; !it.equals(end); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract end(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the end element.
* @example
* const rBegin = container.rBegin();
* const rEnd = container.rEnd();
* for (const it = rBegin; !it.equals(rEnd); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract rBegin(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the super begin like c++.
* @example
* const rBegin = container.rBegin();
* const rEnd = container.rEnd();
* for (const it = rBegin; !it.equals(rEnd); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract rEnd(): ContainerIterator<T>;
/**
* @returns The first element of the container.
*/
abstract front(): T | undefined;
/**
* @returns The last element of the container.
*/
abstract back(): T | undefined;
/**
* @param element - The element you want to find.
* @returns An iterator pointing to the element if found, or super end if not found.
* @example
* container.find(1).equals(container.end());
*/
abstract find(element: T): ContainerIterator<T>;
/**
* @description Iterate over all elements in the container.
* @param callback - Callback function like Array.forEach.
* @example
* container.forEach((element, index) => console.log(element, index));
*/
abstract forEach(callback: (element: T, index: number, container: Container<T>) => void): void;
/**
* @description Gets the value of the element at the specified position.
* @example
* const val = container.getElementByPos(-1); // throw a RangeError
*/
abstract getElementByPos(pos: number): T;
/**
* @description Removes the element at the specified position.
* @param pos - The element's position you want to remove.
* @returns The container length after erasing.
* @example
* container.eraseElementByPos(-1); // throw a RangeError
*/
abstract eraseElementByPos(pos: number): number;
/**
* @description Removes element by iterator and move `iter` to next.
* @param iter - The iterator you want to erase.
* @returns The next iterator.
* @example
* container.eraseElementByIterator(container.begin());
* container.eraseElementByIterator(container.end()); // throw a RangeError
*/
abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
/**
* @description Using for `for...of` syntax like Array.
* @example
* for (const element of container) {
* console.log(element);
* }
*/
abstract [Symbol.iterator](): Generator<T, void>;
}
/**
* @description The initial data type passed in when initializing the container.
*/
type initContainer<T> = {
size?: number | (() => number);
length?: number;
forEach: (callback: (el: T) => void) => void;
};
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;
isAccessible(): boolean;
// @ts-ignore
pre(): this;
// @ts-ignore
next(): this;
}
declare const enum TreeNodeColor {
RED = 1,
BLACK = 0
}
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, color?: TreeNodeColor);
/**
* @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>;
}
declare abstract class TreeContainer<K, V> extends Container<K | [
K,
V
]> {
enableIndex: boolean;
protected _inOrderTraversal(): TreeNode<K, V>[];
protected _inOrderTraversal(pos: number): TreeNode<K, V>;
protected _inOrderTraversal(callback: (node: TreeNode<K, V>, index: number, map: this) => void): TreeNode<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>;
/**
* @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>;
}
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>;
// @ts-ignore
equals(iter: OrderedMapIterator<K, V>): boolean;
}
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>;
forEach(callback: (element: [
K,
V
], index: number, map: OrderedMap<K, V>) => void): void;
/**
* @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;
getElementByPos(pos: number): [
K,
V
];
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>;
// @ts-ignore
eraseElementByIterator(iter: OrderedMapIterator<K, V>): OrderedMapIterator<K, V>;
}
export { OrderedMap };
export type { OrderedMapIterator, IteratorType, Container, ContainerIterator, TreeContainer };

View File

@@ -0,0 +1,795 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
class TreeNode {
constructor(t, e, s = 1) {
this.i = undefined;
this.h = undefined;
this.o = undefined;
this.u = t;
this.l = e;
this.p = s;
}
I() {
let t = this;
const e = t.o.o === t;
if (e && t.p === 1) {
t = t.h;
} else if (t.i) {
t = t.i;
while (t.h) {
t = t.h;
}
} else {
if (e) {
return t.o;
}
let s = t.o;
while (s.i === t) {
t = s;
s = t.o;
}
t = s;
}
return t;
}
B() {
let t = this;
if (t.h) {
t = t.h;
while (t.i) {
t = t.i;
}
return t;
} else {
let e = t.o;
while (e.h === t) {
t = e;
e = t.o;
}
if (t.h !== e) {
return e;
} else return t;
}
}
_() {
const t = this.o;
const e = this.h;
const s = e.i;
if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
e.o = t;
e.i = this;
this.o = e;
this.h = s;
if (s) s.o = this;
return e;
}
g() {
const t = this.o;
const e = this.i;
const s = e.h;
if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
e.o = t;
e.h = this;
this.o = e;
this.i = s;
if (s) s.o = this;
return e;
}
}
class TreeNodeEnableIndex extends TreeNode {
constructor() {
super(...arguments);
this.M = 1;
}
_() {
const t = super._();
this.O();
t.O();
return t;
}
g() {
const t = super.g();
this.O();
t.O();
return t;
}
O() {
this.M = 1;
if (this.i) {
this.M += this.i.M;
}
if (this.h) {
this.M += this.h.M;
}
}
}
class ContainerIterator {
constructor(t = 0) {
this.iteratorType = t;
}
equals(t) {
return this.T === t.T;
}
}
class Base {
constructor() {
this.m = 0;
}
get length() {
return this.m;
}
size() {
return this.m;
}
empty() {
return this.m === 0;
}
}
class Container extends Base {}
function throwIteratorAccessError() {
throw new RangeError("Iterator access denied!");
}
class TreeContainer extends Container {
constructor(t = function(t, e) {
if (t < e) return -1;
if (t > e) return 1;
return 0;
}, e = false) {
super();
this.v = undefined;
this.A = t;
this.enableIndex = e;
this.N = e ? TreeNodeEnableIndex : TreeNode;
this.C = new this.N;
}
R(t, e) {
let s = this.C;
while (t) {
const i = this.A(t.u, e);
if (i < 0) {
t = t.h;
} else if (i > 0) {
s = t;
t = t.i;
} else return t;
}
return s;
}
K(t, e) {
let s = this.C;
while (t) {
const i = this.A(t.u, e);
if (i <= 0) {
t = t.h;
} else {
s = t;
t = t.i;
}
}
return s;
}
L(t, e) {
let s = this.C;
while (t) {
const i = this.A(t.u, e);
if (i < 0) {
s = t;
t = t.h;
} else if (i > 0) {
t = t.i;
} else return t;
}
return s;
}
k(t, e) {
let s = this.C;
while (t) {
const i = this.A(t.u, e);
if (i < 0) {
s = t;
t = t.h;
} else {
t = t.i;
}
}
return s;
}
P(t) {
while (true) {
const e = t.o;
if (e === this.C) return;
if (t.p === 1) {
t.p = 0;
return;
}
if (t === e.i) {
const s = e.h;
if (s.p === 1) {
s.p = 0;
e.p = 1;
if (e === this.v) {
this.v = e._();
} else e._();
} else {
if (s.h && s.h.p === 1) {
s.p = e.p;
e.p = 0;
s.h.p = 0;
if (e === this.v) {
this.v = e._();
} else e._();
return;
} else if (s.i && s.i.p === 1) {
s.p = 1;
s.i.p = 0;
s.g();
} else {
s.p = 1;
t = e;
}
}
} else {
const s = e.i;
if (s.p === 1) {
s.p = 0;
e.p = 1;
if (e === this.v) {
this.v = e.g();
} else e.g();
} else {
if (s.i && s.i.p === 1) {
s.p = e.p;
e.p = 0;
s.i.p = 0;
if (e === this.v) {
this.v = e.g();
} else e.g();
return;
} else if (s.h && s.h.p === 1) {
s.p = 1;
s.h.p = 0;
s._();
} else {
s.p = 1;
t = e;
}
}
}
}
}
S(t) {
if (this.m === 1) {
this.clear();
return;
}
let e = t;
while (e.i || e.h) {
if (e.h) {
e = e.h;
while (e.i) e = e.i;
} else {
e = e.i;
}
const s = t.u;
t.u = e.u;
e.u = s;
const i = t.l;
t.l = e.l;
e.l = i;
t = e;
}
if (this.C.i === e) {
this.C.i = e.o;
} else if (this.C.h === e) {
this.C.h = e.o;
}
this.P(e);
let s = e.o;
if (e === s.i) {
s.i = undefined;
} else s.h = undefined;
this.m -= 1;
this.v.p = 0;
if (this.enableIndex) {
while (s !== this.C) {
s.M -= 1;
s = s.o;
}
}
}
U(t) {
const e = typeof t === "number" ? t : undefined;
const s = typeof t === "function" ? t : undefined;
const i = typeof t === "undefined" ? [] : undefined;
let r = 0;
let n = this.v;
const h = [];
while (h.length || n) {
if (n) {
h.push(n);
n = n.i;
} else {
n = h.pop();
if (r === e) return n;
i && i.push(n);
s && s(n, r, this);
r += 1;
n = n.h;
}
}
return i;
}
j(t) {
while (true) {
const e = t.o;
if (e.p === 0) return;
const s = e.o;
if (e === s.i) {
const i = s.h;
if (i && i.p === 1) {
i.p = e.p = 0;
if (s === this.v) return;
s.p = 1;
t = s;
continue;
} else if (t === e.h) {
t.p = 0;
if (t.i) {
t.i.o = e;
}
if (t.h) {
t.h.o = s;
}
e.h = t.i;
s.i = t.h;
t.i = e;
t.h = s;
if (s === this.v) {
this.v = t;
this.C.o = t;
} else {
const e = s.o;
if (e.i === s) {
e.i = t;
} else e.h = t;
}
t.o = s.o;
e.o = t;
s.o = t;
s.p = 1;
} else {
e.p = 0;
if (s === this.v) {
this.v = s.g();
} else s.g();
s.p = 1;
return;
}
} else {
const i = s.i;
if (i && i.p === 1) {
i.p = e.p = 0;
if (s === this.v) return;
s.p = 1;
t = s;
continue;
} else if (t === e.i) {
t.p = 0;
if (t.i) {
t.i.o = s;
}
if (t.h) {
t.h.o = e;
}
s.h = t.i;
e.i = t.h;
t.i = s;
t.h = e;
if (s === this.v) {
this.v = t;
this.C.o = t;
} else {
const e = s.o;
if (e.i === s) {
e.i = t;
} else e.h = t;
}
t.o = s.o;
e.o = t;
s.o = t;
s.p = 1;
} else {
e.p = 0;
if (s === this.v) {
this.v = s._();
} else s._();
s.p = 1;
return;
}
}
if (this.enableIndex) {
e.O();
s.O();
t.O();
}
return;
}
}
q(t, e, s) {
if (this.v === undefined) {
this.m += 1;
this.v = new this.N(t, e, 0);
this.v.o = this.C;
this.C.o = this.C.i = this.C.h = this.v;
return this.m;
}
let i;
const r = this.C.i;
const n = this.A(r.u, t);
if (n === 0) {
r.l = e;
return this.m;
} else if (n > 0) {
r.i = new this.N(t, e);
r.i.o = r;
i = r.i;
this.C.i = i;
} else {
const r = this.C.h;
const n = this.A(r.u, t);
if (n === 0) {
r.l = e;
return this.m;
} else if (n < 0) {
r.h = new this.N(t, e);
r.h.o = r;
i = r.h;
this.C.h = i;
} else {
if (s !== undefined) {
const r = s.T;
if (r !== this.C) {
const s = this.A(r.u, t);
if (s === 0) {
r.l = e;
return this.m;
} else if (s > 0) {
const s = r.I();
const n = this.A(s.u, t);
if (n === 0) {
s.l = e;
return this.m;
} else if (n < 0) {
i = new this.N(t, e);
if (s.h === undefined) {
s.h = i;
i.o = s;
} else {
r.i = i;
i.o = r;
}
}
}
}
}
if (i === undefined) {
i = this.v;
while (true) {
const s = this.A(i.u, t);
if (s > 0) {
if (i.i === undefined) {
i.i = new this.N(t, e);
i.i.o = i;
i = i.i;
break;
}
i = i.i;
} else if (s < 0) {
if (i.h === undefined) {
i.h = new this.N(t, e);
i.h.o = i;
i = i.h;
break;
}
i = i.h;
} else {
i.l = e;
return this.m;
}
}
}
}
}
if (this.enableIndex) {
let t = i.o;
while (t !== this.C) {
t.M += 1;
t = t.o;
}
}
this.j(i);
this.m += 1;
return this.m;
}
H(t, e) {
while (t) {
const s = this.A(t.u, e);
if (s < 0) {
t = t.h;
} else if (s > 0) {
t = t.i;
} else return t;
}
return t || this.C;
}
clear() {
this.m = 0;
this.v = undefined;
this.C.o = undefined;
this.C.i = this.C.h = undefined;
}
updateKeyByIterator(t, e) {
const s = t.T;
if (s === this.C) {
throwIteratorAccessError();
}
if (this.m === 1) {
s.u = e;
return true;
}
const i = s.B().u;
if (s === this.C.i) {
if (this.A(i, e) > 0) {
s.u = e;
return true;
}
return false;
}
const r = s.I().u;
if (s === this.C.h) {
if (this.A(r, e) < 0) {
s.u = e;
return true;
}
return false;
}
if (this.A(r, e) >= 0 || this.A(i, e) <= 0) return false;
s.u = e;
return true;
}
eraseElementByPos(t) {
if (t < 0 || t > this.m - 1) {
throw new RangeError;
}
const e = this.U(t);
this.S(e);
return this.m;
}
eraseElementByKey(t) {
if (this.m === 0) return false;
const e = this.H(this.v, t);
if (e === this.C) return false;
this.S(e);
return true;
}
eraseElementByIterator(t) {
const e = t.T;
if (e === this.C) {
throwIteratorAccessError();
}
const s = e.h === undefined;
const i = t.iteratorType === 0;
if (i) {
if (s) t.next();
} else {
if (!s || e.i === undefined) t.next();
}
this.S(e);
return t;
}
getHeight() {
if (this.m === 0) return 0;
function traversal(t) {
if (!t) return 0;
return Math.max(traversal(t.i), traversal(t.h)) + 1;
}
return traversal(this.v);
}
}
class TreeIterator extends ContainerIterator {
constructor(t, e, s) {
super(s);
this.T = t;
this.C = e;
if (this.iteratorType === 0) {
this.pre = function() {
if (this.T === this.C.i) {
throwIteratorAccessError();
}
this.T = this.T.I();
return this;
};
this.next = function() {
if (this.T === this.C) {
throwIteratorAccessError();
}
this.T = this.T.B();
return this;
};
} else {
this.pre = function() {
if (this.T === this.C.h) {
throwIteratorAccessError();
}
this.T = this.T.B();
return this;
};
this.next = function() {
if (this.T === this.C) {
throwIteratorAccessError();
}
this.T = this.T.I();
return this;
};
}
}
get index() {
let t = this.T;
const e = this.C.o;
if (t === this.C) {
if (e) {
return e.M - 1;
}
return 0;
}
let s = 0;
if (t.i) {
s += t.i.M;
}
while (t !== e) {
const e = t.o;
if (t === e.h) {
s += 1;
if (e.i) {
s += e.i.M;
}
}
t = e;
}
return s;
}
isAccessible() {
return this.T !== this.C;
}
}
class OrderedMapIterator extends TreeIterator {
constructor(t, e, s, i) {
super(t, e, i);
this.container = s;
}
get pointer() {
if (this.T === this.C) {
throwIteratorAccessError();
}
const t = this;
return new Proxy([], {
get(e, s) {
if (s === "0") return t.T.u; else if (s === "1") return t.T.l;
e[0] = t.T.u;
e[1] = t.T.l;
return e[s];
},
set(e, s, i) {
if (s !== "1") {
throw new TypeError("prop must be 1");
}
t.T.l = i;
return true;
}
});
}
copy() {
return new OrderedMapIterator(this.T, this.C, this.container, this.iteratorType);
}
}
class OrderedMap extends TreeContainer {
constructor(t = [], e, s) {
super(e, s);
const i = this;
t.forEach((function(t) {
i.setElement(t[0], t[1]);
}));
}
begin() {
return new OrderedMapIterator(this.C.i || this.C, this.C, this);
}
end() {
return new OrderedMapIterator(this.C, this.C, this);
}
rBegin() {
return new OrderedMapIterator(this.C.h || this.C, this.C, this, 1);
}
rEnd() {
return new OrderedMapIterator(this.C, this.C, this, 1);
}
front() {
if (this.m === 0) return;
const t = this.C.i;
return [ t.u, t.l ];
}
back() {
if (this.m === 0) return;
const t = this.C.h;
return [ t.u, t.l ];
}
lowerBound(t) {
const e = this.R(this.v, t);
return new OrderedMapIterator(e, this.C, this);
}
upperBound(t) {
const e = this.K(this.v, t);
return new OrderedMapIterator(e, this.C, this);
}
reverseLowerBound(t) {
const e = this.L(this.v, t);
return new OrderedMapIterator(e, this.C, this);
}
reverseUpperBound(t) {
const e = this.k(this.v, t);
return new OrderedMapIterator(e, this.C, this);
}
forEach(t) {
this.U((function(e, s, i) {
t([ e.u, e.l ], s, i);
}));
}
setElement(t, e, s) {
return this.q(t, e, s);
}
getElementByPos(t) {
if (t < 0 || t > this.m - 1) {
throw new RangeError;
}
const e = this.U(t);
return [ e.u, e.l ];
}
find(t) {
const e = this.H(this.v, t);
return new OrderedMapIterator(e, this.C, this);
}
getElementByKey(t) {
const e = this.H(this.v, t);
return e.l;
}
union(t) {
const e = this;
t.forEach((function(t) {
e.setElement(t[0], t[1]);
}));
return this.m;
}
* [Symbol.iterator]() {
const t = this.m;
const e = this.U();
for (let s = 0; s < t; ++s) {
const t = e[s];
yield [ t.u, t.l ];
}
}
}
exports.OrderedMap = OrderedMap;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,402 @@
/**
* @description The iterator type including `NORMAL` and `REVERSE`.
*/
declare const enum IteratorType {
NORMAL = 0,
REVERSE = 1
}
declare abstract class ContainerIterator<T> {
/**
* @description The container pointed to by the iterator.
*/
abstract readonly container: Container<T>;
/**
* @description Iterator's type.
* @example
* console.log(container.end().iteratorType === IteratorType.NORMAL); // true
*/
readonly iteratorType: IteratorType;
/**
* @param iter - The other iterator you want to compare.
* @returns Whether this equals to obj.
* @example
* container.find(1).equals(container.end());
*/
equals(iter: ContainerIterator<T>): boolean;
/**
* @description Pointers to element.
* @returns The value of the pointer's element.
* @example
* const val = container.begin().pointer;
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue - The new value you want to set.
* @example
* (<LinkList<number>>container).begin().pointer = 1;
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [0, 1]
* const pre = iter.pre();
* console.log(pre === iter); // true
* console.log(pre.equals(iter)); // true
* console.log(pre.pointer, iter.pointer); // 0, 0
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.next();
* console.log(next === iter); // true
* console.log(next.equals(iter)); // true
* console.log(next.pointer, iter.pointer); // 2, 2
*/
abstract next(): this;
/**
* @description Get a copy of itself.
* @returns The copy of self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.copy().next();
* console.log(next === iter); // false
* console.log(next.equals(iter)); // false
* console.log(next.pointer, iter.pointer); // 2, 1
*/
abstract copy(): ContainerIterator<T>;
abstract isAccessible(): boolean;
}
declare abstract class Base {
/**
* @returns The size of the container.
* @example
* const container = new Vector([1, 2]);
* console.log(container.length); // 2
*/
get length(): number;
/**
* @returns The size of the container.
* @example
* const container = new Vector([1, 2]);
* console.log(container.size()); // 2
*/
size(): number;
/**
* @returns Whether the container is empty.
* @example
* container.clear();
* console.log(container.empty()); // true
*/
empty(): boolean;
/**
* @description Clear the container.
* @example
* container.clear();
* console.log(container.empty()); // true
*/
abstract clear(): void;
}
declare abstract class Container<T> extends Base {
/**
* @returns Iterator pointing to the beginning element.
* @example
* const begin = container.begin();
* const end = container.end();
* for (const it = begin; !it.equals(end); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract begin(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the super end like c++.
* @example
* const begin = container.begin();
* const end = container.end();
* for (const it = begin; !it.equals(end); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract end(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the end element.
* @example
* const rBegin = container.rBegin();
* const rEnd = container.rEnd();
* for (const it = rBegin; !it.equals(rEnd); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract rBegin(): ContainerIterator<T>;
/**
* @returns Iterator pointing to the super begin like c++.
* @example
* const rBegin = container.rBegin();
* const rEnd = container.rEnd();
* for (const it = rBegin; !it.equals(rEnd); it.next()) {
* doSomething(it.pointer);
* }
*/
abstract rEnd(): ContainerIterator<T>;
/**
* @returns The first element of the container.
*/
abstract front(): T | undefined;
/**
* @returns The last element of the container.
*/
abstract back(): T | undefined;
/**
* @param element - The element you want to find.
* @returns An iterator pointing to the element if found, or super end if not found.
* @example
* container.find(1).equals(container.end());
*/
abstract find(element: T): ContainerIterator<T>;
/**
* @description Iterate over all elements in the container.
* @param callback - Callback function like Array.forEach.
* @example
* container.forEach((element, index) => console.log(element, index));
*/
abstract forEach(callback: (element: T, index: number, container: Container<T>) => void): void;
/**
* @description Gets the value of the element at the specified position.
* @example
* const val = container.getElementByPos(-1); // throw a RangeError
*/
abstract getElementByPos(pos: number): T;
/**
* @description Removes the element at the specified position.
* @param pos - The element's position you want to remove.
* @returns The container length after erasing.
* @example
* container.eraseElementByPos(-1); // throw a RangeError
*/
abstract eraseElementByPos(pos: number): number;
/**
* @description Removes element by iterator and move `iter` to next.
* @param iter - The iterator you want to erase.
* @returns The next iterator.
* @example
* container.eraseElementByIterator(container.begin());
* container.eraseElementByIterator(container.end()); // throw a RangeError
*/
abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
/**
* @description Using for `for...of` syntax like Array.
* @example
* for (const element of container) {
* console.log(element);
* }
*/
abstract [Symbol.iterator](): Generator<T, void>;
}
/**
* @description The initial data type passed in when initializing the container.
*/
type initContainer<T> = {
size?: number | (() => number);
length?: number;
forEach: (callback: (el: T) => void) => void;
};
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;
isAccessible(): boolean;
// @ts-ignore
pre(): this;
// @ts-ignore
next(): this;
}
declare const enum TreeNodeColor {
RED = 1,
BLACK = 0
}
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, color?: TreeNodeColor);
/**
* @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>;
}
declare abstract class TreeContainer<K, V> extends Container<K | [
K,
V
]> {
enableIndex: boolean;
protected _inOrderTraversal(): TreeNode<K, V>[];
protected _inOrderTraversal(pos: number): TreeNode<K, V>;
protected _inOrderTraversal(callback: (node: TreeNode<K, V>, index: number, map: this) => void): TreeNode<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>;
/**
* @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>;
}
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>;
// @ts-ignore
equals(iter: OrderedMapIterator<K, V>): boolean;
}
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>;
forEach(callback: (element: [
K,
V
], index: number, map: OrderedMap<K, V>) => void): void;
/**
* @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;
getElementByPos(pos: number): [
K,
V
];
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>;
// @ts-ignore
eraseElementByIterator(iter: OrderedMapIterator<K, V>): OrderedMapIterator<K, V>;
}
export { OrderedMap };
export type { OrderedMapIterator, IteratorType, Container, ContainerIterator, TreeContainer };

View File

@@ -0,0 +1,975 @@
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);
};
function __extends(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 __);
}
function __generator(e, r) {
var t = {
label: 0,
sent: function() {
if (s[0] & 1) throw s[1];
return s[1];
},
trys: [],
ops: []
}, i, n, s, h;
return h = {
next: verb(0),
throw: verb(1),
return: verb(2)
}, typeof Symbol === "function" && (h[Symbol.iterator] = function() {
return this;
}), h;
function verb(e) {
return function(r) {
return step([ e, r ]);
};
}
function step(a) {
if (i) throw new TypeError("Generator is already executing.");
while (h && (h = 0, a[0] && (t = 0)), t) try {
if (i = 1, n && (s = a[0] & 2 ? n["return"] : a[0] ? n["throw"] || ((s = n["return"]) && s.call(n),
0) : n.next) && !(s = s.call(n, a[1])).done) return s;
if (n = 0, s) a = [ a[0] & 2, s.value ];
switch (a[0]) {
case 0:
case 1:
s = a;
break;
case 4:
t.label++;
return {
value: a[1],
done: false
};
case 5:
t.label++;
n = a[1];
a = [ 0 ];
continue;
case 7:
a = t.ops.pop();
t.trys.pop();
continue;
default:
if (!(s = t.trys, s = s.length > 0 && s[s.length - 1]) && (a[0] === 6 || a[0] === 2)) {
t = 0;
continue;
}
if (a[0] === 3 && (!s || a[1] > s[0] && a[1] < s[3])) {
t.label = a[1];
break;
}
if (a[0] === 6 && t.label < s[1]) {
t.label = s[1];
s = a;
break;
}
if (s && t.label < s[2]) {
t.label = s[2];
t.ops.push(a);
break;
}
if (s[2]) t.ops.pop();
t.trys.pop();
continue;
}
a = r.call(e, t);
} catch (e) {
a = [ 6, e ];
n = 0;
} finally {
i = s = 0;
}
if (a[0] & 5) throw a[1];
return {
value: a[0] ? a[1] : void 0,
done: true
};
}
}
typeof SuppressedError === "function" ? SuppressedError : function(e, r, t) {
var i = new Error(t);
return i.name = "SuppressedError", i.error = e, i.suppressed = r, i;
};
var TreeNode = function() {
function TreeNode(e, r, t) {
if (t === void 0) {
t = 1;
}
this.t = undefined;
this.i = undefined;
this.h = undefined;
this.u = e;
this.o = r;
this.l = t;
}
TreeNode.prototype.v = function() {
var e = this;
var r = e.h.h === e;
if (r && e.l === 1) {
e = e.i;
} else if (e.t) {
e = e.t;
while (e.i) {
e = e.i;
}
} else {
if (r) {
return e.h;
}
var t = e.h;
while (t.t === e) {
e = t;
t = e.h;
}
e = t;
}
return e;
};
TreeNode.prototype.p = function() {
var e = this;
if (e.i) {
e = e.i;
while (e.t) {
e = e.t;
}
return e;
} else {
var r = e.h;
while (r.i === e) {
e = r;
r = e.h;
}
if (e.i !== r) {
return r;
} else return e;
}
};
TreeNode.prototype.T = function() {
var e = this.h;
var r = this.i;
var t = r.t;
if (e.h === this) e.h = r; else if (e.t === this) e.t = r; else e.i = r;
r.h = e;
r.t = this;
this.h = r;
this.i = t;
if (t) t.h = this;
return r;
};
TreeNode.prototype.I = function() {
var e = this.h;
var r = this.t;
var t = r.i;
if (e.h === this) e.h = r; else if (e.t === this) e.t = r; else e.i = r;
r.h = e;
r.i = this;
this.h = r;
this.t = t;
if (t) t.h = this;
return r;
};
return TreeNode;
}();
var TreeNodeEnableIndex = function(e) {
__extends(TreeNodeEnableIndex, e);
function TreeNodeEnableIndex() {
var r = e !== null && e.apply(this, arguments) || this;
r.O = 1;
return r;
}
TreeNodeEnableIndex.prototype.T = function() {
var r = e.prototype.T.call(this);
this.M();
r.M();
return r;
};
TreeNodeEnableIndex.prototype.I = function() {
var r = e.prototype.I.call(this);
this.M();
r.M();
return r;
};
TreeNodeEnableIndex.prototype.M = function() {
this.O = 1;
if (this.t) {
this.O += this.t.O;
}
if (this.i) {
this.O += this.i.O;
}
};
return TreeNodeEnableIndex;
}(TreeNode);
var ContainerIterator = function() {
function ContainerIterator(e) {
if (e === void 0) {
e = 0;
}
this.iteratorType = e;
}
ContainerIterator.prototype.equals = function(e) {
return this.C === e.C;
};
return ContainerIterator;
}();
var Base = function() {
function Base() {
this._ = 0;
}
Object.defineProperty(Base.prototype, "length", {
get: function() {
return this._;
},
enumerable: false,
configurable: true
});
Base.prototype.size = function() {
return this._;
};
Base.prototype.empty = function() {
return this._ === 0;
};
return Base;
}();
var Container = function(e) {
__extends(Container, e);
function Container() {
return e !== null && e.apply(this, arguments) || this;
}
return Container;
}(Base);
function throwIteratorAccessError() {
throw new RangeError("Iterator access denied!");
}
var TreeContainer = function(e) {
__extends(TreeContainer, e);
function TreeContainer(r, t) {
if (r === void 0) {
r = function(e, r) {
if (e < r) return -1;
if (e > r) return 1;
return 0;
};
}
if (t === void 0) {
t = false;
}
var i = e.call(this) || this;
i.N = undefined;
i.g = r;
i.enableIndex = t;
i.S = t ? TreeNodeEnableIndex : TreeNode;
i.A = new i.S;
return i;
}
TreeContainer.prototype.m = function(e, r) {
var t = this.A;
while (e) {
var i = this.g(e.u, r);
if (i < 0) {
e = e.i;
} else if (i > 0) {
t = e;
e = e.t;
} else return e;
}
return t;
};
TreeContainer.prototype.B = function(e, r) {
var t = this.A;
while (e) {
var i = this.g(e.u, r);
if (i <= 0) {
e = e.i;
} else {
t = e;
e = e.t;
}
}
return t;
};
TreeContainer.prototype.j = function(e, r) {
var t = this.A;
while (e) {
var i = this.g(e.u, r);
if (i < 0) {
t = e;
e = e.i;
} else if (i > 0) {
e = e.t;
} else return e;
}
return t;
};
TreeContainer.prototype.k = function(e, r) {
var t = this.A;
while (e) {
var i = this.g(e.u, r);
if (i < 0) {
t = e;
e = e.i;
} else {
e = e.t;
}
}
return t;
};
TreeContainer.prototype.R = function(e) {
while (true) {
var r = e.h;
if (r === this.A) return;
if (e.l === 1) {
e.l = 0;
return;
}
if (e === r.t) {
var t = r.i;
if (t.l === 1) {
t.l = 0;
r.l = 1;
if (r === this.N) {
this.N = r.T();
} else r.T();
} else {
if (t.i && t.i.l === 1) {
t.l = r.l;
r.l = 0;
t.i.l = 0;
if (r === this.N) {
this.N = r.T();
} else r.T();
return;
} else if (t.t && t.t.l === 1) {
t.l = 1;
t.t.l = 0;
t.I();
} else {
t.l = 1;
e = r;
}
}
} else {
var t = r.t;
if (t.l === 1) {
t.l = 0;
r.l = 1;
if (r === this.N) {
this.N = r.I();
} else r.I();
} else {
if (t.t && t.t.l === 1) {
t.l = r.l;
r.l = 0;
t.t.l = 0;
if (r === this.N) {
this.N = r.I();
} else r.I();
return;
} else if (t.i && t.i.l === 1) {
t.l = 1;
t.i.l = 0;
t.T();
} else {
t.l = 1;
e = r;
}
}
}
}
};
TreeContainer.prototype.G = function(e) {
if (this._ === 1) {
this.clear();
return;
}
var r = e;
while (r.t || r.i) {
if (r.i) {
r = r.i;
while (r.t) r = r.t;
} else {
r = r.t;
}
var t = e.u;
e.u = r.u;
r.u = t;
var i = e.o;
e.o = r.o;
r.o = i;
e = r;
}
if (this.A.t === r) {
this.A.t = r.h;
} else if (this.A.i === r) {
this.A.i = r.h;
}
this.R(r);
var n = r.h;
if (r === n.t) {
n.t = undefined;
} else n.i = undefined;
this._ -= 1;
this.N.l = 0;
if (this.enableIndex) {
while (n !== this.A) {
n.O -= 1;
n = n.h;
}
}
};
TreeContainer.prototype.P = function(e) {
var r = typeof e === "number" ? e : undefined;
var t = typeof e === "function" ? e : undefined;
var i = typeof e === "undefined" ? [] : undefined;
var n = 0;
var s = this.N;
var h = [];
while (h.length || s) {
if (s) {
h.push(s);
s = s.t;
} else {
s = h.pop();
if (n === r) return s;
i && i.push(s);
t && t(s, n, this);
n += 1;
s = s.i;
}
}
return i;
};
TreeContainer.prototype.q = function(e) {
while (true) {
var r = e.h;
if (r.l === 0) return;
var t = r.h;
if (r === t.t) {
var i = t.i;
if (i && i.l === 1) {
i.l = r.l = 0;
if (t === this.N) return;
t.l = 1;
e = t;
continue;
} else if (e === r.i) {
e.l = 0;
if (e.t) {
e.t.h = r;
}
if (e.i) {
e.i.h = t;
}
r.i = e.t;
t.t = e.i;
e.t = r;
e.i = t;
if (t === this.N) {
this.N = e;
this.A.h = e;
} else {
var n = t.h;
if (n.t === t) {
n.t = e;
} else n.i = e;
}
e.h = t.h;
r.h = e;
t.h = e;
t.l = 1;
} else {
r.l = 0;
if (t === this.N) {
this.N = t.I();
} else t.I();
t.l = 1;
return;
}
} else {
var i = t.t;
if (i && i.l === 1) {
i.l = r.l = 0;
if (t === this.N) return;
t.l = 1;
e = t;
continue;
} else if (e === r.t) {
e.l = 0;
if (e.t) {
e.t.h = t;
}
if (e.i) {
e.i.h = r;
}
t.i = e.t;
r.t = e.i;
e.t = t;
e.i = r;
if (t === this.N) {
this.N = e;
this.A.h = e;
} else {
var n = t.h;
if (n.t === t) {
n.t = e;
} else n.i = e;
}
e.h = t.h;
r.h = e;
t.h = e;
t.l = 1;
} else {
r.l = 0;
if (t === this.N) {
this.N = t.T();
} else t.T();
t.l = 1;
return;
}
}
if (this.enableIndex) {
r.M();
t.M();
e.M();
}
return;
}
};
TreeContainer.prototype.D = function(e, r, t) {
if (this.N === undefined) {
this._ += 1;
this.N = new this.S(e, r, 0);
this.N.h = this.A;
this.A.h = this.A.t = this.A.i = this.N;
return this._;
}
var i;
var n = this.A.t;
var s = this.g(n.u, e);
if (s === 0) {
n.o = r;
return this._;
} else if (s > 0) {
n.t = new this.S(e, r);
n.t.h = n;
i = n.t;
this.A.t = i;
} else {
var h = this.A.i;
var a = this.g(h.u, e);
if (a === 0) {
h.o = r;
return this._;
} else if (a < 0) {
h.i = new this.S(e, r);
h.i.h = h;
i = h.i;
this.A.i = i;
} else {
if (t !== undefined) {
var u = t.C;
if (u !== this.A) {
var f = this.g(u.u, e);
if (f === 0) {
u.o = r;
return this._;
} else if (f > 0) {
var o = u.v();
var d = this.g(o.u, e);
if (d === 0) {
o.o = r;
return this._;
} else if (d < 0) {
i = new this.S(e, r);
if (o.i === undefined) {
o.i = i;
i.h = o;
} else {
u.t = i;
i.h = u;
}
}
}
}
}
if (i === undefined) {
i = this.N;
while (true) {
var c = this.g(i.u, e);
if (c > 0) {
if (i.t === undefined) {
i.t = new this.S(e, r);
i.t.h = i;
i = i.t;
break;
}
i = i.t;
} else if (c < 0) {
if (i.i === undefined) {
i.i = new this.S(e, r);
i.i.h = i;
i = i.i;
break;
}
i = i.i;
} else {
i.o = r;
return this._;
}
}
}
}
}
if (this.enableIndex) {
var l = i.h;
while (l !== this.A) {
l.O += 1;
l = l.h;
}
}
this.q(i);
this._ += 1;
return this._;
};
TreeContainer.prototype.F = function(e, r) {
while (e) {
var t = this.g(e.u, r);
if (t < 0) {
e = e.i;
} else if (t > 0) {
e = e.t;
} else return e;
}
return e || this.A;
};
TreeContainer.prototype.clear = function() {
this._ = 0;
this.N = undefined;
this.A.h = undefined;
this.A.t = this.A.i = undefined;
};
TreeContainer.prototype.updateKeyByIterator = function(e, r) {
var t = e.C;
if (t === this.A) {
throwIteratorAccessError();
}
if (this._ === 1) {
t.u = r;
return true;
}
var i = t.p().u;
if (t === this.A.t) {
if (this.g(i, r) > 0) {
t.u = r;
return true;
}
return false;
}
var n = t.v().u;
if (t === this.A.i) {
if (this.g(n, r) < 0) {
t.u = r;
return true;
}
return false;
}
if (this.g(n, r) >= 0 || this.g(i, r) <= 0) return false;
t.u = r;
return true;
};
TreeContainer.prototype.eraseElementByPos = function(e) {
if (e < 0 || e > this._ - 1) {
throw new RangeError;
}
var r = this.P(e);
this.G(r);
return this._;
};
TreeContainer.prototype.eraseElementByKey = function(e) {
if (this._ === 0) return false;
var r = this.F(this.N, e);
if (r === this.A) return false;
this.G(r);
return true;
};
TreeContainer.prototype.eraseElementByIterator = function(e) {
var r = e.C;
if (r === this.A) {
throwIteratorAccessError();
}
var t = r.i === undefined;
var i = e.iteratorType === 0;
if (i) {
if (t) e.next();
} else {
if (!t || r.t === undefined) e.next();
}
this.G(r);
return e;
};
TreeContainer.prototype.getHeight = function() {
if (this._ === 0) return 0;
function traversal(e) {
if (!e) return 0;
return Math.max(traversal(e.t), traversal(e.i)) + 1;
}
return traversal(this.N);
};
return TreeContainer;
}(Container);
var TreeIterator = function(e) {
__extends(TreeIterator, e);
function TreeIterator(r, t, i) {
var n = e.call(this, i) || this;
n.C = r;
n.A = t;
if (n.iteratorType === 0) {
n.pre = function() {
if (this.C === this.A.t) {
throwIteratorAccessError();
}
this.C = this.C.v();
return this;
};
n.next = function() {
if (this.C === this.A) {
throwIteratorAccessError();
}
this.C = this.C.p();
return this;
};
} else {
n.pre = function() {
if (this.C === this.A.i) {
throwIteratorAccessError();
}
this.C = this.C.p();
return this;
};
n.next = function() {
if (this.C === this.A) {
throwIteratorAccessError();
}
this.C = this.C.v();
return this;
};
}
return n;
}
Object.defineProperty(TreeIterator.prototype, "index", {
get: function() {
var e = this.C;
var r = this.A.h;
if (e === this.A) {
if (r) {
return r.O - 1;
}
return 0;
}
var t = 0;
if (e.t) {
t += e.t.O;
}
while (e !== r) {
var i = e.h;
if (e === i.i) {
t += 1;
if (i.t) {
t += i.t.O;
}
}
e = i;
}
return t;
},
enumerable: false,
configurable: true
});
TreeIterator.prototype.isAccessible = function() {
return this.C !== this.A;
};
return TreeIterator;
}(ContainerIterator);
var OrderedMapIterator = function(e) {
__extends(OrderedMapIterator, e);
function OrderedMapIterator(r, t, i, n) {
var s = e.call(this, r, t, n) || this;
s.container = i;
return s;
}
Object.defineProperty(OrderedMapIterator.prototype, "pointer", {
get: function() {
if (this.C === this.A) {
throwIteratorAccessError();
}
var e = this;
return new Proxy([], {
get: function(r, t) {
if (t === "0") return e.C.u; else if (t === "1") return e.C.o;
r[0] = e.C.u;
r[1] = e.C.o;
return r[t];
},
set: function(r, t, i) {
if (t !== "1") {
throw new TypeError("prop must be 1");
}
e.C.o = i;
return true;
}
});
},
enumerable: false,
configurable: true
});
OrderedMapIterator.prototype.copy = function() {
return new OrderedMapIterator(this.C, this.A, this.container, this.iteratorType);
};
return OrderedMapIterator;
}(TreeIterator);
var OrderedMap = function(e) {
__extends(OrderedMap, e);
function OrderedMap(r, t, i) {
if (r === void 0) {
r = [];
}
var n = e.call(this, t, i) || this;
var s = n;
r.forEach((function(e) {
s.setElement(e[0], e[1]);
}));
return n;
}
OrderedMap.prototype.begin = function() {
return new OrderedMapIterator(this.A.t || this.A, this.A, this);
};
OrderedMap.prototype.end = function() {
return new OrderedMapIterator(this.A, this.A, this);
};
OrderedMap.prototype.rBegin = function() {
return new OrderedMapIterator(this.A.i || this.A, this.A, this, 1);
};
OrderedMap.prototype.rEnd = function() {
return new OrderedMapIterator(this.A, this.A, this, 1);
};
OrderedMap.prototype.front = function() {
if (this._ === 0) return;
var e = this.A.t;
return [ e.u, e.o ];
};
OrderedMap.prototype.back = function() {
if (this._ === 0) return;
var e = this.A.i;
return [ e.u, e.o ];
};
OrderedMap.prototype.lowerBound = function(e) {
var r = this.m(this.N, e);
return new OrderedMapIterator(r, this.A, this);
};
OrderedMap.prototype.upperBound = function(e) {
var r = this.B(this.N, e);
return new OrderedMapIterator(r, this.A, this);
};
OrderedMap.prototype.reverseLowerBound = function(e) {
var r = this.j(this.N, e);
return new OrderedMapIterator(r, this.A, this);
};
OrderedMap.prototype.reverseUpperBound = function(e) {
var r = this.k(this.N, e);
return new OrderedMapIterator(r, this.A, this);
};
OrderedMap.prototype.forEach = function(e) {
this.P((function(r, t, i) {
e([ r.u, r.o ], t, i);
}));
};
OrderedMap.prototype.setElement = function(e, r, t) {
return this.D(e, r, t);
};
OrderedMap.prototype.getElementByPos = function(e) {
if (e < 0 || e > this._ - 1) {
throw new RangeError;
}
var r = this.P(e);
return [ r.u, r.o ];
};
OrderedMap.prototype.find = function(e) {
var r = this.F(this.N, e);
return new OrderedMapIterator(r, this.A, this);
};
OrderedMap.prototype.getElementByKey = function(e) {
var r = this.F(this.N, e);
return r.o;
};
OrderedMap.prototype.union = function(e) {
var r = this;
e.forEach((function(e) {
r.setElement(e[0], e[1]);
}));
return this._;
};
OrderedMap.prototype[Symbol.iterator] = function() {
var e, r, t, i;
return __generator(this, (function(n) {
switch (n.label) {
case 0:
e = this._;
r = this.P();
t = 0;
n.label = 1;
case 1:
if (!(t < e)) return [ 3, 4 ];
i = r[t];
return [ 4, [ i.u, i.o ] ];
case 2:
n.sent();
n.label = 3;
case 3:
++t;
return [ 3, 1 ];
case 4:
return [ 2 ];
}
}));
};
return OrderedMap;
}(TreeContainer);
export { OrderedMap };
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long