Insane number of adds to setup env and packages

This commit is contained in:
2022-11-07 17:45:54 -05:00
parent 62c2bb73a9
commit 55ecf095a9
3127 changed files with 485223 additions and 1 deletions

View File

@@ -0,0 +1,120 @@
export declare const enum IteratorType {
NORMAL = 0,
REVERSE = 1
}
export declare abstract class ContainerIterator<T> {
/**
* @description Iterator's type.
*/
readonly iteratorType: IteratorType;
protected constructor(iteratorType?: IteratorType);
/**
* @description Pointers to element.
* @return The value of the pointer's element.
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue The new value you want to set.
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
*/
abstract next(): this;
/**
* @param obj The other iterator you want to compare.
* @return Boolean about if this equals to obj.
* @example container.find(1).equals(container.end());
*/
abstract equals(obj: ContainerIterator<T>): boolean;
/**
* @description Get a copy of itself.<br/>
* We do not guarantee the safety of this function.<br/>
* Please ensure that the iterator will not fail.
* @return The copy of self.
*/
abstract copy(): ContainerIterator<T>;
}
export declare abstract class Base {
/**
* @return The size of the container.
*/
size(): number;
/**
* @return Boolean about if the container is empty.
*/
empty(): boolean;
/**
* @description Clear the container.
*/
abstract clear(): void;
}
export declare abstract class Container<T> extends Base {
/**
* @return Iterator pointing to the beginning element.
*/
abstract begin(): ContainerIterator<T>;
/**
* @return Iterator pointing to the super end like c++.
*/
abstract end(): ContainerIterator<T>;
/**
* @return Iterator pointing to the end element.
*/
abstract rBegin(): ContainerIterator<T>;
/**
* @return Iterator pointing to the super begin like c++.
*/
abstract rEnd(): ContainerIterator<T>;
/**
* @return The first element of the container.
*/
abstract front(): T | undefined;
/**
* @return The last element of the container.
*/
abstract back(): T | undefined;
/**
* @description Iterate over all elements in the container.
* @param callback Callback function like Array.forEach.
*/
abstract forEach(callback: (element: T, index: number) => void): void;
/**
* @param element The element you want to find.
* @return An iterator pointing to the element if found, or super end if not found.
*/
abstract find(element: T): ContainerIterator<T>;
/**
* @description Gets the value of the element at the specified position.
*/
abstract getElementByPos(pos: number): T;
/**
* @description Removes the element at the specified position.
* @param pos The element's position you want to remove.
*/
abstract eraseElementByPos(pos: number): void;
/**
* @description Removes element by iterator and move `iter` to next.
* @param iter The iterator you want to erase.
* @example container.eraseElementByIterator(container.begin());
*/
abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
/**
* @description Using for `for...of` syntax like Array.
*/
abstract [Symbol.iterator](): Generator<T, void, undefined>;
}
export declare type initContainer<T> = ({
size: number;
} | {
length: number;
} | {
size(): number;
}) & {
forEach(callback: (element: T) => void): void;
};

View File

@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.ContainerIterator = exports.Container = exports.Base = void 0;
class ContainerIterator {
constructor(t = 0) {
this.iteratorType = t;
}
}
exports.ContainerIterator = ContainerIterator;
class Base {
constructor() {
this.o = 0;
}
size() {
return this.o;
}
empty() {
return this.o === 0;
}
}
exports.Base = Base;
class Container extends Base {}
exports.Container = Container;

View File

@@ -0,0 +1,25 @@
import { Base } from "../../ContainerBase";
declare abstract class HashContainer<K> extends Base {
protected constructor(initBucketNum?: number, hashFunc?: (x: K) => number);
clear(): void;
/**
* @description Iterate over all elements in the container.
* @param callback Callback function like Array.forEach.
*/
abstract forEach(callback: (element: unknown, index: number) => void): void;
/**
* @description Remove the elements of the specified value.
* @param key The element you want to remove.
*/
abstract eraseElementByKey(key: K): void;
/**
* @param key The element you want to find.
* @return Boolean about if the specified element in the hash set.
*/
abstract find(key: K): void;
/**
* @description Using for `for...of` syntax like Array.
*/
abstract [Symbol.iterator](): Generator<K | [K, unknown], void, undefined>;
}
export default HashContainer;

View File

@@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../../ContainerBase");
class HashContainer extends _ContainerBase.Base {
constructor(e = 16, t = (e => {
let t;
if (typeof e !== "string") {
t = JSON.stringify(e);
} else t = e;
let r = 0;
const s = t.length;
for (let e = 0; e < s; e++) {
const s = t.charCodeAt(e);
r = (r << 5) - r + s;
r |= 0;
}
return r >>> 0;
})) {
super();
if (e < 16 || (e & e - 1) !== 0) {
throw new RangeError("InitBucketNum range error");
}
this.u = this.te = e;
this.l = t;
}
clear() {
this.o = 0;
this.u = this.te;
this.i = [];
}
}
var _default = HashContainer;
exports.default = _default;

View File

@@ -0,0 +1,22 @@
import { initContainer } from "../ContainerBase";
import HashContainer from './Base';
declare class HashMap<K, V> extends HashContainer<K> {
constructor(container?: initContainer<[K, V]>, initBucketNum?: number, hashFunc?: (x: K) => number);
forEach(callback: (element: [K, V], index: number) => void): void;
/**
* @description Insert a new key-value pair to hash map or set value by key.
* @param key The key you want to insert.
* @param value The value you want to insert.
* @example HashMap.setElement(1, 2); // insert a key-value pair [1, 2]
*/
setElement(key: K, value: V): void;
/**
* @description Get the value of the element which has the specified key.
* @param key The key you want to get.
*/
getElementByKey(key: K): V | undefined;
eraseElementByKey(key: K): void;
find(key: K): boolean;
[Symbol.iterator](): Generator<[K, V], void, unknown>;
}
export default HashMap;

View File

@@ -0,0 +1,178 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _Vector = _interopRequireDefault(require("../SequentialContainer/Vector"));
var _OrderedMap = _interopRequireDefault(require("../TreeContainer/OrderedMap"));
function _interopRequireDefault(e) {
return e && e.t ? e : {
default: e
};
}
class HashMap extends _Base.default {
constructor(e = [], t, s) {
super(t, s);
this.i = [];
e.forEach((e => this.setElement(e[0], e[1])));
}
h() {
if (this.u >= 1073741824) return;
const e = [];
const t = this.u;
this.u <<= 1;
const s = Object.keys(this.i);
const r = s.length;
for (let n = 0; n < r; ++n) {
const r = parseInt(s[n]);
const i = this.i[r];
const o = i.size();
if (o === 0) continue;
if (o === 1) {
const t = i.front();
e[this.l(t[0]) & this.u - 1] = new _Vector.default([ t ], false);
continue;
}
const c = [];
const f = [];
i.forEach((e => {
const s = this.l(e[0]);
if ((s & t) === 0) {
c.push(e);
} else f.push(e);
}));
if (i instanceof _OrderedMap.default) {
if (c.length > 6) {
e[r] = new _OrderedMap.default(c);
} else {
e[r] = new _Vector.default(c, false);
}
if (f.length > 6) {
e[r + t] = new _OrderedMap.default(f);
} else {
e[r + t] = new _Vector.default(f, false);
}
} else {
e[r] = new _Vector.default(c, false);
e[r + t] = new _Vector.default(f, false);
}
}
this.i = e;
}
forEach(e) {
const t = Object.values(this.i);
const s = t.length;
let r = 0;
for (let n = 0; n < s; ++n) {
t[n].forEach((t => e(t, r++)));
}
}
setElement(e, t) {
const s = this.l(e) & this.u - 1;
const r = this.i[s];
if (!r) {
this.o += 1;
this.i[s] = new _Vector.default([ [ e, t ] ], false);
} else {
const n = r.size();
if (r instanceof _Vector.default) {
for (const s of r) {
if (s[0] === e) {
s[1] = t;
return;
}
}
r.pushBack([ e, t ]);
if (n + 1 >= 8) {
if (this.u <= 64) {
this.o += 1;
this.h();
return;
}
this.i[s] = new _OrderedMap.default(this.i[s]);
}
this.o += 1;
} else {
r.setElement(e, t);
const s = r.size();
this.o += s - n;
}
}
if (this.o > this.u * .75) {
this.h();
}
}
getElementByKey(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) return undefined;
if (s instanceof _OrderedMap.default) {
return s.getElementByKey(e);
} else {
for (const t of s) {
if (t[0] === e) return t[1];
}
return undefined;
}
}
eraseElementByKey(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) return;
if (s instanceof _Vector.default) {
let t = 0;
for (const r of s) {
if (r[0] === e) {
s.eraseElementByPos(t);
this.o -= 1;
return;
}
t += 1;
}
} else {
const r = s.size();
s.eraseElementByKey(e);
const n = s.size();
this.o += n - r;
if (n <= 6) {
this.i[t] = new _Vector.default(s);
}
}
}
find(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) return false;
if (s instanceof _OrderedMap.default) {
return !s.find(e).equals(s.end());
}
for (const t of s) {
if (t[0] === e) return true;
}
return false;
}
[Symbol.iterator]() {
return function*() {
const e = Object.values(this.i);
const t = e.length;
for (let s = 0; s < t; ++s) {
const t = e[s];
for (const e of t) {
yield e;
}
}
}.bind(this)();
}
}
var _default = HashMap;
exports.default = _default;

View File

@@ -0,0 +1,15 @@
import HashContainer from './Base';
import { initContainer } from "../ContainerBase";
declare class HashSet<K> extends HashContainer<K> {
constructor(container?: initContainer<K>, initBucketNum?: number, _hashFunc?: (x: K) => number);
forEach(callback: (element: K, index: number) => void): void;
/**
* @description Insert element to hash set.
* @param element The element you want to insert.
*/
insert(element: K): void;
eraseElementByKey(key: K): void;
find(element: K): boolean;
[Symbol.iterator](): Generator<K, void, unknown>;
}
export default HashSet;

View File

@@ -0,0 +1,149 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _Vector = _interopRequireDefault(require("../SequentialContainer/Vector"));
var _OrderedSet = _interopRequireDefault(require("../TreeContainer/OrderedSet"));
function _interopRequireDefault(e) {
return e && e.t ? e : {
default: e
};
}
class HashSet extends _Base.default {
constructor(e = [], t, s) {
super(t, s);
this.i = [];
e.forEach((e => this.insert(e)));
}
h() {
if (this.u >= 1073741824) return;
const e = [];
const t = this.u;
this.u <<= 1;
const s = Object.keys(this.i);
const i = s.length;
for (let r = 0; r < i; ++r) {
const i = parseInt(s[r]);
const n = this.i[i];
const o = n.size();
if (o === 0) continue;
if (o === 1) {
const t = n.front();
e[this.l(t) & this.u - 1] = new _Vector.default([ t ], false);
continue;
}
const c = [];
const f = [];
n.forEach((e => {
const s = this.l(e);
if ((s & t) === 0) {
c.push(e);
} else f.push(e);
}));
if (n instanceof _OrderedSet.default) {
if (c.length > 6) {
e[i] = new _OrderedSet.default(c);
} else {
e[i] = new _Vector.default(c, false);
}
if (f.length > 6) {
e[i + t] = new _OrderedSet.default(f);
} else {
e[i + t] = new _Vector.default(f, false);
}
} else {
e[i] = new _Vector.default(c, false);
e[i + t] = new _Vector.default(f, false);
}
}
this.i = e;
}
forEach(e) {
const t = Object.values(this.i);
const s = t.length;
let i = 0;
for (let r = 0; r < s; ++r) {
t[r].forEach((t => e(t, i++)));
}
}
insert(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) {
this.i[t] = new _Vector.default([ e ], false);
this.o += 1;
} else {
const i = s.size();
if (s instanceof _Vector.default) {
if (!s.find(e).equals(s.end())) return;
s.pushBack(e);
if (i + 1 >= 8) {
if (this.u <= 64) {
this.o += 1;
this.h();
return;
}
this.i[t] = new _OrderedSet.default(s);
}
this.o += 1;
} else {
s.insert(e);
const t = s.size();
this.o += t - i;
}
}
if (this.o > this.u * .75) {
this.h();
}
}
eraseElementByKey(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) return;
const i = s.size();
if (i === 0) return;
if (s instanceof _Vector.default) {
s.eraseElementByValue(e);
const t = s.size();
this.o += t - i;
} else {
s.eraseElementByKey(e);
const r = s.size();
this.o += r - i;
if (r <= 6) {
this.i[t] = new _Vector.default(s);
}
}
}
find(e) {
const t = this.l(e) & this.u - 1;
const s = this.i[t];
if (!s) return false;
return !s.find(e).equals(s.end());
}
[Symbol.iterator]() {
return function*() {
const e = Object.values(this.i);
const t = e.length;
for (let s = 0; s < t; ++s) {
const t = e[s];
for (const e of t) {
yield e;
}
}
}.bind(this)();
}
}
var _default = HashSet;
exports.default = _default;

View File

@@ -0,0 +1,48 @@
import { Base, initContainer } from "../ContainerBase";
declare class PriorityQueue<T> extends Base {
/**
* @description PriorityQueue's constructor.
* @param container Initialize container, must have a forEach function.
* @param cmp Compare function.
* @param copy When the container is an array, you can choose to directly operate on the original object of
* the array or perform a shallow copy. The default is shallow copy.
*/
constructor(container?: initContainer<T>, cmp?: (x: T, y: T) => number, copy?: boolean);
clear(): void;
/**
* @description Push element into a container in order.
* @param item The element you want to push.
*/
push(item: T): void;
/**
* @description Removes the top element.
*/
pop(): void;
/**
* @description Accesses the top element.
*/
top(): T | undefined;
/**
* @description Check if element is in heap.
* @param item The item want to find.
* @return Boolean about if element is in heap.
*/
find(item: T): boolean;
/**
* @description Remove specified item from heap.
* @param item The item want to remove.
* @return Boolean about if remove success.
*/
remove(item: T): boolean;
/**
* @description Update item and it's pos in the heap.
* @param item The item want to update.
* @return Boolean about if update success.
*/
updateItem(item: T): boolean;
/**
* @return Return a copy array of heap.
*/
toArray(): T[];
}
export default PriorityQueue;

View File

@@ -0,0 +1,112 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../ContainerBase");
class PriorityQueue extends _ContainerBase.Base {
constructor(t = [], s = ((t, s) => {
if (t > s) return -1;
if (t < s) return 1;
return 0;
}), i = true) {
super();
this.p = s;
if (Array.isArray(t)) {
this._ = i ? [ ...t ] : t;
} else {
this._ = [];
t.forEach((t => this._.push(t)));
}
this.o = this._.length;
const e = this.o >> 1;
for (let t = this.o - 1 >> 1; t >= 0; --t) {
this.v(t, e);
}
}
B(t) {
const s = this._[t];
while (t > 0) {
const i = t - 1 >> 1;
const e = this._[i];
if (this.p(e, s) <= 0) break;
this._[t] = e;
t = i;
}
this._[t] = s;
}
v(t, s) {
const i = this._[t];
while (t < s) {
let s = t << 1 | 1;
const e = s + 1;
let h = this._[s];
if (e < this.o && this.p(h, this._[e]) > 0) {
s = e;
h = this._[e];
}
if (this.p(h, i) >= 0) break;
this._[t] = h;
t = s;
}
this._[t] = i;
}
clear() {
this.o = 0;
this._.length = 0;
}
push(t) {
this._.push(t);
this.B(this.o);
this.o += 1;
}
pop() {
if (!this.o) return;
const t = this._.pop();
this.o -= 1;
if (this.o) {
this._[0] = t;
this.v(0, this.o >> 1);
}
}
top() {
return this._[0];
}
find(t) {
return this._.indexOf(t) >= 0;
}
remove(t) {
const s = this._.indexOf(t);
if (s < 0) return false;
if (s === 0) {
this.pop();
} else if (s === this.o - 1) {
this._.pop();
this.o -= 1;
} else {
this._.splice(s, 1, this._.pop());
this.o -= 1;
this.B(s);
this.v(s, this.o >> 1);
}
return true;
}
updateItem(t) {
const s = this._.indexOf(t);
if (s < 0) return false;
this.B(s);
this.v(s, this.o >> 1);
return true;
}
toArray() {
return [ ...this._ ];
}
}
var _default = PriorityQueue;
exports.default = _default;

View File

@@ -0,0 +1,18 @@
import { Base, initContainer } from "../ContainerBase";
declare class Queue<T> extends Base {
constructor(container?: initContainer<T>);
clear(): void;
/**
* @description Inserts element to queue's end.
*/
push(element: T): void;
/**
* @description Removes the first element.
*/
pop(): void;
/**
* @description Access the first element.
*/
front(): T | undefined;
}
export default Queue;

View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _Deque = _interopRequireDefault(require("../SequentialContainer/Deque"));
var _ContainerBase = require("../ContainerBase");
function _interopRequireDefault(e) {
return e && e.t ? e : {
default: e
};
}
class Queue extends _ContainerBase.Base {
constructor(e = []) {
super();
this.q = new _Deque.default(e);
this.o = this.q.size();
}
clear() {
this.q.clear();
this.o = 0;
}
push(e) {
this.q.pushBack(e);
this.o += 1;
}
pop() {
this.q.popFront();
if (this.o) this.o -= 1;
}
front() {
return this.q.front();
}
}
var _default = Queue;
exports.default = _default;

View File

@@ -0,0 +1,18 @@
import { Base, initContainer } from "../ContainerBase";
declare class Stack<T> extends Base {
constructor(container?: initContainer<T>);
clear(): void;
/**
* @description Insert element to stack's end.
*/
push(element: T): void;
/**
* @description Removes the end element.
*/
pop(): void;
/**
* @description Accesses the end element.
*/
top(): T | undefined;
}
export default Stack;

View File

@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../ContainerBase");
class Stack extends _ContainerBase.Base {
constructor(t = []) {
super();
this.C = [];
t.forEach((t => this.push(t)));
}
clear() {
this.o = 0;
this.C.length = 0;
}
push(t) {
this.C.push(t);
this.o += 1;
}
pop() {
this.C.pop();
if (this.o > 0) this.o -= 1;
}
top() {
return this.C[this.o - 1];
}
}
var _default = Stack;
exports.default = _default;

View File

@@ -0,0 +1,8 @@
import { ContainerIterator } from "../../ContainerBase";
export declare abstract class RandomIterator<T> extends ContainerIterator<T> {
pre: () => this;
next: () => this;
get pointer(): T;
set pointer(newValue: T);
equals(obj: RandomIterator<T>): boolean;
}

View File

@@ -0,0 +1,67 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.RandomIterator = void 0;
var _ContainerBase = require("../../ContainerBase");
class RandomIterator extends _ContainerBase.ContainerIterator {
constructor(t, r, e, i, s) {
super(s);
this.I = t;
this.D = r;
this.g = e;
this.m = i;
if (this.iteratorType === 0) {
this.pre = function() {
if (this.I === 0) {
throw new RangeError("Random iterator access denied!");
}
this.I -= 1;
return this;
};
this.next = function() {
if (this.I === this.D()) {
throw new RangeError("Random Iterator access denied!");
}
this.I += 1;
return this;
};
} else {
this.pre = function() {
if (this.I === this.D() - 1) {
throw new RangeError("Random iterator access denied!");
}
this.I += 1;
return this;
};
this.next = function() {
if (this.I === -1) {
throw new RangeError("Random iterator access denied!");
}
this.I -= 1;
return this;
};
}
}
get pointer() {
if (this.I < 0 || this.I > this.D() - 1) {
throw new RangeError;
}
return this.g(this.I);
}
set pointer(t) {
if (this.I < 0 || this.I > this.D() - 1) {
throw new RangeError;
}
this.m(this.I, t);
}
equals(t) {
return this.I === t.I;
}
}
exports.RandomIterator = RandomIterator;

View File

@@ -0,0 +1,44 @@
import { Container } from "../../ContainerBase";
declare abstract class SequentialContainer<T> extends Container<T> {
/**
* @description Push the element to the back.
* @param element The element you want to push.
*/
abstract pushBack(element: T): void;
/**
* @description Removes the last element.
*/
abstract popBack(): void;
/**
* @description Sets element by position.
* @param pos The position you want to change.
* @param element The element's value you want to update.
*/
abstract setElementByPos(pos: number, element: T): void;
/**
* @description Removes the elements of the specified value.
* @param value The value you want to remove.
*/
abstract eraseElementByValue(value: T): void;
/**
* @description Insert several elements after the specified position.
* @param pos The position you want to insert.
* @param element The element you want to insert.
* @param num The number of elements you want to insert (default 1).
*/
abstract insert(pos: number, element: T, num?: number): void;
/**
* @description Reverses the container.
*/
abstract reverse(): void;
/**
* @description Removes the duplication of elements in the container.
*/
abstract unique(): void;
/**
* @description Sort the container.
* @param cmp Comparison function.
*/
abstract sort(cmp?: (x: T, y: T) => number): void;
}
export default SequentialContainer;

View File

@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../../ContainerBase");
class SequentialContainer extends _ContainerBase.Container {}
var _default = SequentialContainer;
exports.default = _default;

View File

@@ -0,0 +1,50 @@
import SequentialContainer from './Base';
import { initContainer } from "../ContainerBase";
import { RandomIterator } from "./Base/RandomIterator";
export declare class DequeIterator<T> extends RandomIterator<T> {
copy(): DequeIterator<T>;
}
declare class Deque<T> extends SequentialContainer<T> {
constructor(container?: initContainer<T>, _bucketSize?: number);
clear(): void;
front(): T | undefined;
back(): T | undefined;
begin(): DequeIterator<T>;
end(): DequeIterator<T>;
rBegin(): DequeIterator<T>;
rEnd(): DequeIterator<T>;
pushBack(element: T): void;
popBack(): void;
/**
* @description Push the element to the front.
* @param element The element you want to push.
*/
pushFront(element: T): void;
/**
* @description Remove the _first element.
*/
popFront(): void;
forEach(callback: (element: T, index: number) => void): void;
getElementByPos(pos: number): T;
setElementByPos(pos: number, element: T): void;
insert(pos: number, element: T, num?: number): void;
/**
* @description Remove all elements after the specified position (excluding the specified position).
* @param pos The previous position of the _first removed element.
* @example deque.cut(1); // Then deque's size will be 2. deque -> [0, 1]
*/
cut(pos: number): void;
eraseElementByPos(pos: number): void;
eraseElementByValue(value: T): void;
eraseElementByIterator(iter: DequeIterator<T>): DequeIterator<T>;
find(element: T): DequeIterator<T>;
reverse(): void;
unique(): void;
sort(cmp?: (x: T, y: T) => number): void;
/**
* @description Remove as much useless space as possible.
*/
shrinkToFit(): void;
[Symbol.iterator](): Generator<T, void, unknown>;
}
export default Deque;

View File

@@ -0,0 +1,324 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = exports.DequeIterator = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _RandomIterator = require("./Base/RandomIterator");
function _interopRequireDefault(t) {
return t && t.t ? t : {
default: t
};
}
class DequeIterator extends _RandomIterator.RandomIterator {
copy() {
return new DequeIterator(this.I, this.D, this.g, this.m, this.iteratorType);
}
}
exports.DequeIterator = DequeIterator;
class Deque extends _Base.default {
constructor(t = [], i = 1 << 12) {
super();
this.R = 0;
this.k = 0;
this.N = 0;
this.M = 0;
this.u = 0;
this.P = [];
let s;
if ("size" in t) {
if (typeof t.size === "number") {
s = t.size;
} else {
s = t.size();
}
} else if ("length" in t) {
s = t.length;
} else {
throw new RangeError("Can't get container's size!");
}
this.A = i;
this.u = Math.max(Math.ceil(s / this.A), 1);
for (let t = 0; t < this.u; ++t) {
this.P.push(new Array(this.A));
}
const h = Math.ceil(s / this.A);
this.R = this.N = (this.u >> 1) - (h >> 1);
this.k = this.M = this.A - s % this.A >> 1;
t.forEach((t => this.pushBack(t)));
this.size = this.size.bind(this);
this.getElementByPos = this.getElementByPos.bind(this);
this.setElementByPos = this.setElementByPos.bind(this);
}
h() {
const t = [];
const i = Math.max(this.u >> 1, 1);
for (let s = 0; s < i; ++s) {
t[s] = new Array(this.A);
}
for (let i = this.R; i < this.u; ++i) {
t[t.length] = this.P[i];
}
for (let i = 0; i < this.N; ++i) {
t[t.length] = this.P[i];
}
t[t.length] = [ ...this.P[this.N] ];
this.R = i;
this.N = t.length - 1;
for (let s = 0; s < i; ++s) {
t[t.length] = new Array(this.A);
}
this.P = t;
this.u = t.length;
}
F(t) {
const i = this.k + t + 1;
const s = i % this.A;
let h = s - 1;
let e = this.R + (i - s) / this.A;
if (s === 0) e -= 1;
e %= this.u;
if (h < 0) h += this.A;
return {
curNodeBucketIndex: e,
curNodePointerIndex: h
};
}
clear() {
this.P = [ [] ];
this.u = 1;
this.R = this.N = this.o = 0;
this.k = this.M = this.A >> 1;
}
front() {
return this.P[this.R][this.k];
}
back() {
return this.P[this.N][this.M];
}
begin() {
return new DequeIterator(0, this.size, this.getElementByPos, this.setElementByPos);
}
end() {
return new DequeIterator(this.o, this.size, this.getElementByPos, this.setElementByPos);
}
rBegin() {
return new DequeIterator(this.o - 1, this.size, this.getElementByPos, this.setElementByPos, 1);
}
rEnd() {
return new DequeIterator(-1, this.size, this.getElementByPos, this.setElementByPos, 1);
}
pushBack(t) {
if (this.o) {
if (this.M < this.A - 1) {
this.M += 1;
} else if (this.N < this.u - 1) {
this.N += 1;
this.M = 0;
} else {
this.N = 0;
this.M = 0;
}
if (this.N === this.R && this.M === this.k) this.h();
}
this.o += 1;
this.P[this.N][this.M] = t;
}
popBack() {
if (!this.o) return;
this.P[this.N][this.M] = undefined;
if (this.o !== 1) {
if (this.M > 0) {
this.M -= 1;
} else if (this.N > 0) {
this.N -= 1;
this.M = this.A - 1;
} else {
this.N = this.u - 1;
this.M = this.A - 1;
}
}
this.o -= 1;
}
pushFront(t) {
if (this.o) {
if (this.k > 0) {
this.k -= 1;
} else if (this.R > 0) {
this.R -= 1;
this.k = this.A - 1;
} else {
this.R = this.u - 1;
this.k = this.A - 1;
}
if (this.R === this.N && this.k === this.M) this.h();
}
this.o += 1;
this.P[this.R][this.k] = t;
}
popFront() {
if (!this.o) return;
this.P[this.R][this.k] = undefined;
if (this.o !== 1) {
if (this.k < this.A - 1) {
this.k += 1;
} else if (this.R < this.u - 1) {
this.R += 1;
this.k = 0;
} else {
this.R = 0;
this.k = 0;
}
}
this.o -= 1;
}
forEach(t) {
for (let i = 0; i < this.o; ++i) {
t(this.getElementByPos(i), i);
}
}
getElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
const {curNodeBucketIndex: i, curNodePointerIndex: s} = this.F(t);
return this.P[i][s];
}
setElementByPos(t, i) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
const {curNodeBucketIndex: s, curNodePointerIndex: h} = this.F(t);
this.P[s][h] = i;
}
insert(t, i, s = 1) {
if (t < 0 || t > this.o) {
throw new RangeError;
}
if (t === 0) {
while (s--) this.pushFront(i);
} else if (t === this.o) {
while (s--) this.pushBack(i);
} else {
const h = [];
for (let i = t; i < this.o; ++i) {
h.push(this.getElementByPos(i));
}
this.cut(t - 1);
for (let t = 0; t < s; ++t) this.pushBack(i);
for (let t = 0; t < h.length; ++t) this.pushBack(h[t]);
}
}
cut(t) {
if (t < 0) {
this.clear();
return;
}
const {curNodeBucketIndex: i, curNodePointerIndex: s} = this.F(t);
this.N = i;
this.M = s;
this.o = t + 1;
}
eraseElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
if (t === 0) this.popFront(); else if (t === this.o - 1) this.popBack(); else {
const i = [];
for (let s = t + 1; s < this.o; ++s) {
i.push(this.getElementByPos(s));
}
this.cut(t);
this.popBack();
i.forEach((t => this.pushBack(t)));
}
}
eraseElementByValue(t) {
if (!this.o) return;
const i = [];
for (let s = 0; s < this.o; ++s) {
const h = this.getElementByPos(s);
if (h !== t) i.push(h);
}
const s = i.length;
for (let t = 0; t < s; ++t) this.setElementByPos(t, i[t]);
this.cut(s - 1);
}
eraseElementByIterator(t) {
const i = t.I;
this.eraseElementByPos(i);
t = t.next();
return t;
}
find(t) {
for (let i = 0; i < this.o; ++i) {
if (this.getElementByPos(i) === t) {
return new DequeIterator(i, this.size, this.getElementByPos, this.setElementByPos);
}
}
return this.end();
}
reverse() {
let t = 0;
let i = this.o - 1;
while (t < i) {
const s = this.getElementByPos(t);
this.setElementByPos(t, this.getElementByPos(i));
this.setElementByPos(i, s);
t += 1;
i -= 1;
}
}
unique() {
if (this.o <= 1) return;
let t = 1;
let i = this.getElementByPos(0);
for (let s = 1; s < this.o; ++s) {
const h = this.getElementByPos(s);
if (h !== i) {
i = h;
this.setElementByPos(t++, h);
}
}
while (this.o > t) this.popBack();
}
sort(t) {
const i = [];
for (let t = 0; t < this.o; ++t) {
i.push(this.getElementByPos(t));
}
i.sort(t);
for (let t = 0; t < this.o; ++t) this.setElementByPos(t, i[t]);
}
shrinkToFit() {
if (!this.o) return;
const t = [];
this.forEach((i => t.push(i)));
this.u = Math.max(Math.ceil(this.o / this.A), 1);
this.o = this.R = this.N = this.k = this.M = 0;
this.P = [];
for (let t = 0; t < this.u; ++t) {
this.P.push(new Array(this.A));
}
for (let i = 0; i < t.length; ++i) this.pushBack(t[i]);
}
[Symbol.iterator]() {
return function*() {
for (let t = 0; t < this.o; ++t) {
yield this.getElementByPos(t);
}
}.bind(this)();
}
}
var _default = Deque;
exports.default = _default;

View File

@@ -0,0 +1,49 @@
import SequentialContainer from './Base';
import { ContainerIterator, initContainer } from "../ContainerBase";
export declare class LinkListIterator<T> extends ContainerIterator<T> {
pre: () => this;
next: () => this;
get pointer(): T;
set pointer(newValue: T);
equals(obj: LinkListIterator<T>): boolean;
copy(): LinkListIterator<T>;
}
declare class LinkList<T> extends SequentialContainer<T> {
constructor(container?: initContainer<T>);
clear(): void;
begin(): LinkListIterator<T>;
end(): LinkListIterator<T>;
rBegin(): LinkListIterator<T>;
rEnd(): LinkListIterator<T>;
front(): T | undefined;
back(): T | undefined;
forEach(callback: (element: T, index: number) => void): void;
getElementByPos(pos: number): T;
eraseElementByPos(pos: number): void;
eraseElementByValue(_value: T): void;
eraseElementByIterator(iter: LinkListIterator<T>): LinkListIterator<T>;
pushBack(element: T): void;
popBack(): void;
setElementByPos(pos: number, element: T): void;
insert(pos: number, element: T, num?: number): void;
find(element: T): LinkListIterator<T>;
reverse(): void;
unique(): void;
sort(cmp?: (x: T, y: T) => number): void;
/**
* @description Push an element to the front.
* @param element The element you want to push.
*/
pushFront(element: T): void;
/**
* @description Removes the first element.
*/
popFront(): void;
/**
* @description Merges two sorted lists.
* @param list The other list you want to merge (must be sorted).
*/
merge(list: LinkList<T>): void;
[Symbol.iterator](): Generator<T, void, unknown>;
}
export default LinkList;

View File

@@ -0,0 +1,366 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = exports.LinkNode = exports.LinkListIterator = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _ContainerBase = require("../ContainerBase");
function _interopRequireDefault(t) {
return t && t.t ? t : {
default: t
};
}
class LinkNode {
constructor(t) {
this.L = undefined;
this.j = undefined;
this.O = undefined;
this.L = t;
}
}
exports.LinkNode = LinkNode;
class LinkListIterator extends _ContainerBase.ContainerIterator {
constructor(t, i, s) {
super(s);
this.I = t;
this.S = i;
if (this.iteratorType === 0) {
this.pre = function() {
if (this.I.j === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
this.I = this.I.j;
return this;
};
this.next = function() {
if (this.I === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
this.I = this.I.O;
return this;
};
} else {
this.pre = function() {
if (this.I.O === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
this.I = this.I.O;
return this;
};
this.next = function() {
if (this.I === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
this.I = this.I.j;
return this;
};
}
}
get pointer() {
if (this.I === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
return this.I.L;
}
set pointer(t) {
if (this.I === this.S) {
throw new RangeError("LinkList iterator access denied!");
}
this.I.L = t;
}
equals(t) {
return this.I === t.I;
}
copy() {
return new LinkListIterator(this.I, this.S, this.iteratorType);
}
}
exports.LinkListIterator = LinkListIterator;
class LinkList extends _Base.default {
constructor(t = []) {
super();
this.S = new LinkNode;
this.V = undefined;
this.G = undefined;
t.forEach((t => this.pushBack(t)));
}
clear() {
this.o = 0;
this.V = this.G = undefined;
this.S.j = this.S.O = undefined;
}
begin() {
return new LinkListIterator(this.V || this.S, this.S);
}
end() {
return new LinkListIterator(this.S, this.S);
}
rBegin() {
return new LinkListIterator(this.G || this.S, this.S, 1);
}
rEnd() {
return new LinkListIterator(this.S, this.S, 1);
}
front() {
return this.V ? this.V.L : undefined;
}
back() {
return this.G ? this.G.L : undefined;
}
forEach(t) {
if (!this.o) return;
let i = this.V;
let s = 0;
while (i !== this.S) {
t(i.L, s++);
i = i.O;
}
}
getElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
let i = this.V;
while (t--) {
i = i.O;
}
return i.L;
}
eraseElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
if (t === 0) this.popFront(); else if (t === this.o - 1) this.popBack(); else {
let i = this.V;
while (t--) {
i = i.O;
}
i = i;
const s = i.j;
const e = i.O;
e.j = s;
s.O = e;
this.o -= 1;
}
}
eraseElementByValue(t) {
while (this.V && this.V.L === t) this.popFront();
while (this.G && this.G.L === t) this.popBack();
if (!this.V) return;
let i = this.V;
while (i !== this.S) {
if (i.L === t) {
const t = i.j;
const s = i.O;
s.j = t;
t.O = s;
this.o -= 1;
}
i = i.O;
}
}
eraseElementByIterator(t) {
const i = t.I;
if (i === this.S) {
throw new RangeError("Invalid iterator");
}
t = t.next();
if (this.V === i) this.popFront(); else if (this.G === i) this.popBack(); else {
const t = i.j;
const s = i.O;
s.j = t;
t.O = s;
this.o -= 1;
}
return t;
}
pushBack(t) {
this.o += 1;
const i = new LinkNode(t);
if (!this.G) {
this.V = this.G = i;
this.S.O = this.V;
this.V.j = this.S;
} else {
this.G.O = i;
i.j = this.G;
this.G = i;
}
this.G.O = this.S;
this.S.j = this.G;
}
popBack() {
if (!this.G) return;
this.o -= 1;
if (this.V === this.G) {
this.V = this.G = undefined;
this.S.O = undefined;
} else {
this.G = this.G.j;
this.G.O = this.S;
}
this.S.j = this.G;
}
setElementByPos(t, i) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
let s = this.V;
while (t--) {
s = s.O;
}
s.L = i;
}
insert(t, i, s = 1) {
if (t < 0 || t > this.o) {
throw new RangeError;
}
if (s <= 0) return;
if (t === 0) {
while (s--) this.pushFront(i);
} else if (t === this.o) {
while (s--) this.pushBack(i);
} else {
let e = this.V;
for (let i = 1; i < t; ++i) {
e = e.O;
}
const h = e.O;
this.o += s;
while (s--) {
e.O = new LinkNode(i);
e.O.j = e;
e = e.O;
}
e.O = h;
h.j = e;
}
}
find(t) {
if (!this.V) return this.end();
let i = this.V;
while (i !== this.S) {
if (i.L === t) {
return new LinkListIterator(i, this.S);
}
i = i.O;
}
return this.end();
}
reverse() {
if (this.o <= 1) return;
let t = this.V;
let i = this.G;
let s = 0;
while (s << 1 < this.o) {
const e = t.L;
t.L = i.L;
i.L = e;
t = t.O;
i = i.j;
s += 1;
}
}
unique() {
if (this.o <= 1) return;
let t = this.V;
while (t !== this.S) {
let i = t;
while (i.O && i.L === i.O.L) {
i = i.O;
this.o -= 1;
}
t.O = i.O;
t.O.j = t;
t = t.O;
}
}
sort(t) {
if (this.o <= 1) return;
const i = [];
this.forEach((t => i.push(t)));
i.sort(t);
let s = this.V;
i.forEach((t => {
s.L = t;
s = s.O;
}));
}
pushFront(t) {
this.o += 1;
const i = new LinkNode(t);
if (!this.V) {
this.V = this.G = i;
this.G.O = this.S;
this.S.j = this.G;
} else {
i.O = this.V;
this.V.j = i;
this.V = i;
}
this.S.O = this.V;
this.V.j = this.S;
}
popFront() {
if (!this.V) return;
this.o -= 1;
if (this.V === this.G) {
this.V = this.G = undefined;
this.S.j = this.G;
} else {
this.V = this.V.O;
this.V.j = this.S;
}
this.S.O = this.V;
}
merge(t) {
if (!this.V) {
t.forEach((t => this.pushBack(t)));
return;
}
let i = this.V;
t.forEach((t => {
while (i && i !== this.S && i.L <= t) {
i = i.O;
}
if (i === this.S) {
this.pushBack(t);
i = this.G;
} else if (i === this.V) {
this.pushFront(t);
i = this.V;
} else {
this.o += 1;
const s = i.j;
s.O = new LinkNode(t);
s.O.j = s;
s.O.O = i;
i.j = s.O;
}
}));
}
[Symbol.iterator]() {
return function*() {
if (!this.V) return;
let t = this.V;
while (t !== this.S) {
yield t.L;
t = t.O;
}
}.bind(this)();
}
}
var _default = LinkList;
exports.default = _default;

View File

@@ -0,0 +1,37 @@
import SequentialContainer from './Base';
import { initContainer } from "../ContainerBase";
import { RandomIterator } from "./Base/RandomIterator";
export declare class VectorIterator<T> extends RandomIterator<T> {
copy(): VectorIterator<T>;
}
declare class Vector<T> extends SequentialContainer<T> {
/**
* @description Vector's constructor.
* @param container Initialize container, must have a forEach function.
* @param copy When the container is an array, you can choose to directly operate on the original object of
* the array or perform a shallow copy. The default is shallow copy.
*/
constructor(container?: initContainer<T>, copy?: boolean);
clear(): void;
begin(): VectorIterator<T>;
end(): VectorIterator<T>;
rBegin(): VectorIterator<T>;
rEnd(): VectorIterator<T>;
front(): T | undefined;
back(): T | undefined;
forEach(callback: (element: T, index: number) => void): void;
getElementByPos(pos: number): T;
eraseElementByPos(pos: number): void;
eraseElementByValue(value: T): void;
eraseElementByIterator(iter: VectorIterator<T>): VectorIterator<T>;
pushBack(element: T): void;
popBack(): void;
setElementByPos(pos: number, element: T): void;
insert(pos: number, element: T, num?: number): void;
find(element: T): VectorIterator<T>;
reverse(): void;
unique(): void;
sort(cmp?: (x: T, y: T) => number): void;
[Symbol.iterator](): Generator<T, any, undefined>;
}
export default Vector;

View File

@@ -0,0 +1,150 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = exports.VectorIterator = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _RandomIterator = require("./Base/RandomIterator");
function _interopRequireDefault(t) {
return t && t.t ? t : {
default: t
};
}
class VectorIterator extends _RandomIterator.RandomIterator {
copy() {
return new VectorIterator(this.I, this.D, this.g, this.m, this.iteratorType);
}
}
exports.VectorIterator = VectorIterator;
class Vector extends _Base.default {
constructor(t = [], e = true) {
super();
if (Array.isArray(t)) {
this.H = e ? [ ...t ] : t;
this.o = t.length;
} else {
this.H = [];
t.forEach((t => this.pushBack(t)));
}
this.size = this.size.bind(this);
this.getElementByPos = this.getElementByPos.bind(this);
this.setElementByPos = this.setElementByPos.bind(this);
}
clear() {
this.o = 0;
this.H.length = 0;
}
begin() {
return new VectorIterator(0, this.size, this.getElementByPos, this.setElementByPos);
}
end() {
return new VectorIterator(this.o, this.size, this.getElementByPos, this.setElementByPos);
}
rBegin() {
return new VectorIterator(this.o - 1, this.size, this.getElementByPos, this.setElementByPos, 1);
}
rEnd() {
return new VectorIterator(-1, this.size, this.getElementByPos, this.setElementByPos, 1);
}
front() {
return this.H[0];
}
back() {
return this.H[this.o - 1];
}
forEach(t) {
for (let e = 0; e < this.o; ++e) {
t(this.H[e], e);
}
}
getElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
return this.H[t];
}
eraseElementByPos(t) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
this.H.splice(t, 1);
this.o -= 1;
}
eraseElementByValue(t) {
let e = 0;
for (let r = 0; r < this.o; ++r) {
if (this.H[r] !== t) {
this.H[e++] = this.H[r];
}
}
this.o = this.H.length = e;
}
eraseElementByIterator(t) {
const e = t.I;
t = t.next();
this.eraseElementByPos(e);
return t;
}
pushBack(t) {
this.H.push(t);
this.o += 1;
}
popBack() {
if (!this.o) return;
this.H.pop();
this.o -= 1;
}
setElementByPos(t, e) {
if (t < 0 || t > this.o - 1) {
throw new RangeError;
}
this.H[t] = e;
}
insert(t, e, r = 1) {
if (t < 0 || t > this.o) {
throw new RangeError;
}
this.H.splice(t, 0, ...new Array(r).fill(e));
this.o += r;
}
find(t) {
for (let e = 0; e < this.o; ++e) {
if (this.H[e] === t) {
return new VectorIterator(e, this.size, this.getElementByPos, this.getElementByPos);
}
}
return this.end();
}
reverse() {
this.H.reverse();
}
unique() {
let t = 1;
for (let e = 1; e < this.o; ++e) {
if (this.H[e] !== this.H[e - 1]) {
this.H[t++] = this.H[e];
}
}
this.o = this.H.length = t;
}
sort(t) {
this.H.sort(t);
}
[Symbol.iterator]() {
return function*() {
return yield* this.H;
}.bind(this)();
}
}
var _default = Vector;
exports.default = _default;

View File

@@ -0,0 +1,15 @@
import { ContainerIterator } from "../../ContainerBase";
declare abstract class TreeIterator<K, V> extends ContainerIterator<K | [K, V]> {
pre: () => this;
next: () => this;
/**
* @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`.
*/
get index(): number;
equals(obj: TreeIterator<K, V>): boolean;
}
export default TreeIterator;

View File

@@ -0,0 +1,80 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../../ContainerBase");
class TreeIterator extends _ContainerBase.ContainerIterator {
constructor(t, e, r) {
super(r);
this.I = t;
this.S = e;
if (this.iteratorType === 0) {
this.pre = function() {
if (this.I === this.S.U) {
throw new RangeError("Tree iterator access denied!");
}
this.I = this.I.pre();
return this;
};
this.next = function() {
if (this.I === this.S) {
throw new RangeError("Tree iterator access denied!");
}
this.I = this.I.next();
return this;
};
} else {
this.pre = function() {
if (this.I === this.S.J) {
throw new RangeError("Tree iterator access denied!");
}
this.I = this.I.next();
return this;
};
this.next = function() {
if (this.I === this.S) {
throw new RangeError("Tree iterator access denied!");
}
this.I = this.I.pre();
return this;
};
}
}
get index() {
let t = this.I;
const e = this.S.tt;
if (t === this.S) {
if (e) {
return e.et - 1;
}
return 0;
}
let r = 0;
if (t.U) {
r += t.U.et;
}
while (t !== e) {
const e = t.tt;
if (t === e.J) {
r += 1;
if (e.U) {
r += e.U.et;
}
}
t = e;
}
return r;
}
equals(t) {
return this.I === t.I;
}
}
var _default = TreeIterator;
exports.default = _default;

View File

@@ -0,0 +1,36 @@
export declare class TreeNode<K, V> {
constructor(_key?: K, _value?: V);
/**
* @description Get the pre node.
* @return TreeNode about the pre node.
*/
pre(): TreeNode<K, V>;
/**
* @description Get the next node.
* @return TreeNode about the next node.
*/
next(): TreeNode<K, V>;
/**
* @description Rotate _left.
* @return TreeNode about moved to original position after rotation.
*/
rotateLeft(): TreeNode<K, V>;
/**
* @description Rotate _right.
* @return TreeNode about moved to original position after rotation.
*/
rotateRight(): TreeNode<K, V>;
}
export declare class TreeNodeEnableIndex<K, V> extends TreeNode<K, V> {
/**
* @description Rotate _left and do recount.
* @return TreeNode about moved to original position after rotation.
*/
rotateLeft(): TreeNodeEnableIndex<K, V>;
/**
* @description Rotate _right and do recount.
* @return TreeNode about moved to original position after rotation.
*/
rotateRight(): TreeNode<K, V>;
recount(): void;
}

View File

@@ -0,0 +1,113 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.TreeNodeEnableIndex = exports.TreeNode = void 0;
class TreeNode {
constructor(e, t) {
this.se = 1;
this.T = undefined;
this.L = undefined;
this.U = undefined;
this.J = undefined;
this.tt = undefined;
this.T = e;
this.L = t;
}
pre() {
let e = this;
if (e.se === 1 && e.tt.tt === e) {
e = e.J;
} else if (e.U) {
e = e.U;
while (e.J) {
e = e.J;
}
} else {
let t = e.tt;
while (t.U === e) {
e = t;
t = e.tt;
}
e = t;
}
return e;
}
next() {
let e = this;
if (e.J) {
e = e.J;
while (e.U) {
e = e.U;
}
return e;
} else {
let t = e.tt;
while (t.J === e) {
e = t;
t = e.tt;
}
if (e.J !== t) {
return t;
} else return e;
}
}
rotateLeft() {
const e = this.tt;
const t = this.J;
const s = t.U;
if (e.tt === this) e.tt = t; else if (e.U === this) e.U = t; else e.J = t;
t.tt = e;
t.U = this;
this.tt = t;
this.J = s;
if (s) s.tt = this;
return t;
}
rotateRight() {
const e = this.tt;
const t = this.U;
const s = t.J;
if (e.tt === this) e.tt = t; else if (e.U === this) e.U = t; else e.J = t;
t.tt = e;
t.J = this;
this.tt = t;
this.U = s;
if (s) s.tt = this;
return t;
}
}
exports.TreeNode = TreeNode;
class TreeNodeEnableIndex extends TreeNode {
constructor() {
super(...arguments);
this.U = undefined;
this.J = undefined;
this.tt = undefined;
this.et = 1;
}
rotateLeft() {
const e = super.rotateLeft();
this.recount();
e.recount();
return e;
}
rotateRight() {
const e = super.rotateRight();
this.recount();
e.recount();
return e;
}
recount() {
this.et = 1;
if (this.U) this.et += this.U.et;
if (this.J) this.et += this.J.et;
}
}
exports.TreeNodeEnableIndex = TreeNodeEnableIndex;

View File

@@ -0,0 +1,55 @@
import type TreeIterator from './TreeIterator';
import { Container } from "../../ContainerBase";
declare abstract class TreeContainer<K, V> extends Container<K | [K, V]> {
/**
* @param cmp The compare function.
* @param enableIndex Whether to enable iterator indexing function.
*/
protected constructor(cmp?: (x: K, y: K) => number, enableIndex?: boolean);
/**
* @param _key The given _key you want to compare.
* @return An iterator to the first element not less than the given _key.
*/
abstract lowerBound(_key: K): TreeIterator<K, V>;
/**
* @param _key The given _key you want to compare.
* @return An iterator to the first element greater than the given _key.
*/
abstract upperBound(_key: K): TreeIterator<K, V>;
/**
* @param _key The given _key you want to compare.
* @return An iterator to the first element not greater than the given _key.
*/
abstract reverseLowerBound(_key: K): TreeIterator<K, V>;
/**
* @param _key The given _key you want to compare.
* @return An iterator to the first element less than the given _key.
*/
abstract reverseUpperBound(_key: K): TreeIterator<K, V>;
/**
* @description Union the other tree to self.
* @param other The other tree container you want to merge.
*/
abstract union(other: TreeContainer<K, V>): void;
clear(): void;
/**
* @description Update node's _key by iterator.
* @param iter The iterator you want to change.
* @param _key The _key you want to update.
* @return Boolean about if the modification is successful.
*/
updateKeyByIterator(iter: TreeIterator<K, V>, _key: K): boolean;
eraseElementByPos(pos: number): void;
/**
* @description Remove the element of the specified _key.
* @param _key The _key you want to remove.
*/
eraseElementByKey(_key: K): void;
eraseElementByIterator(iter: TreeIterator<K, V>): TreeIterator<K, V>;
/**
* @description Get the height of the tree.
* @return Number about the height of the RB-tree.
*/
getHeight(): number;
}
export default TreeContainer;

View File

@@ -0,0 +1,483 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = void 0;
var _ContainerBase = require("../../ContainerBase");
var _TreeNode = require("./TreeNode");
class TreeContainer extends _ContainerBase.Container {
constructor(e = ((e, t) => {
if (e < t) return -1;
if (e > t) return 1;
return 0;
}), t = false) {
super();
this.X = undefined;
this.ie = (e, t) => {
if (e === undefined) return false;
const i = this.ie(e.U, t);
if (i) return true;
if (t(e)) return true;
return this.ie(e.J, t);
};
this.p = e;
if (t) {
this.ne = _TreeNode.TreeNodeEnableIndex;
this.ee = function(e, t, i) {
const s = this.he(e, t, i);
if (s) {
let e = s.tt;
while (e !== this.S) {
e.et += 1;
e = e.tt;
}
const t = this.fe(s);
if (t) {
const {parentNode: e, grandParent: i, curNode: s} = t;
e.recount();
i.recount();
s.recount();
}
}
};
this.ue = function(e) {
let t = this.le(e);
while (t !== this.S) {
t.et -= 1;
t = t.tt;
}
};
} else {
this.ne = _TreeNode.TreeNode;
this.ee = function(e, t, i) {
const s = this.he(e, t, i);
if (s) this.fe(s);
};
this.ue = this.le;
}
this.S = new this.ne;
}
W(e, t) {
let i;
while (e) {
const s = this.p(e.T, t);
if (s < 0) {
e = e.J;
} else if (s > 0) {
i = e;
e = e.U;
} else return e;
}
return i === undefined ? this.S : i;
}
Y(e, t) {
let i;
while (e) {
const s = this.p(e.T, t);
if (s <= 0) {
e = e.J;
} else {
i = e;
e = e.U;
}
}
return i === undefined ? this.S : i;
}
Z(e, t) {
let i;
while (e) {
const s = this.p(e.T, t);
if (s < 0) {
i = e;
e = e.J;
} else if (s > 0) {
e = e.U;
} else return e;
}
return i === undefined ? this.S : i;
}
$(e, t) {
let i;
while (e) {
const s = this.p(e.T, t);
if (s < 0) {
i = e;
e = e.J;
} else {
e = e.U;
}
}
return i === undefined ? this.S : i;
}
oe(e) {
while (true) {
const t = e.tt;
if (t === this.S) return;
if (e.se === 1) {
e.se = 0;
return;
}
if (e === t.U) {
const i = t.J;
if (i.se === 1) {
i.se = 0;
t.se = 1;
if (t === this.X) {
this.X = t.rotateLeft();
} else t.rotateLeft();
} else {
if (i.J && i.J.se === 1) {
i.se = t.se;
t.se = 0;
i.J.se = 0;
if (t === this.X) {
this.X = t.rotateLeft();
} else t.rotateLeft();
return;
} else if (i.U && i.U.se === 1) {
i.se = 1;
i.U.se = 0;
i.rotateRight();
} else {
i.se = 1;
e = t;
}
}
} else {
const i = t.U;
if (i.se === 1) {
i.se = 0;
t.se = 1;
if (t === this.X) {
this.X = t.rotateRight();
} else t.rotateRight();
} else {
if (i.U && i.U.se === 1) {
i.se = t.se;
t.se = 0;
i.U.se = 0;
if (t === this.X) {
this.X = t.rotateRight();
} else t.rotateRight();
return;
} else if (i.J && i.J.se === 1) {
i.se = 1;
i.J.se = 0;
i.rotateLeft();
} else {
i.se = 1;
e = t;
}
}
}
}
}
le(e) {
if (this.o === 1) {
this.clear();
return this.S;
}
let t = e;
while (t.U || t.J) {
if (t.J) {
t = t.J;
while (t.U) t = t.U;
} else {
t = t.U;
}
[e.T, t.T] = [ t.T, e.T ];
[e.L, t.L] = [ t.L, e.L ];
e = t;
}
if (this.S.U === t) {
this.S.U = t.tt;
} else if (this.S.J === t) {
this.S.J = t.tt;
}
this.oe(t);
const i = t.tt;
if (t === i.U) {
i.U = undefined;
} else i.J = undefined;
this.o -= 1;
this.X.se = 0;
return i;
}
fe(e) {
while (true) {
const t = e.tt;
if (t.se === 0) return;
const i = t.tt;
if (t === i.U) {
const s = i.J;
if (s && s.se === 1) {
s.se = t.se = 0;
if (i === this.X) return;
i.se = 1;
e = i;
continue;
} else if (e === t.J) {
e.se = 0;
if (e.U) e.U.tt = t;
if (e.J) e.J.tt = i;
t.J = e.U;
i.U = e.J;
e.U = t;
e.J = i;
if (i === this.X) {
this.X = e;
this.S.tt = e;
} else {
const t = i.tt;
if (t.U === i) {
t.U = e;
} else t.J = e;
}
e.tt = i.tt;
t.tt = e;
i.tt = e;
i.se = 1;
return {
parentNode: t,
grandParent: i,
curNode: e
};
} else {
t.se = 0;
if (i === this.X) {
this.X = i.rotateRight();
} else i.rotateRight();
i.se = 1;
}
} else {
const s = i.U;
if (s && s.se === 1) {
s.se = t.se = 0;
if (i === this.X) return;
i.se = 1;
e = i;
continue;
} else if (e === t.U) {
e.se = 0;
if (e.U) e.U.tt = i;
if (e.J) e.J.tt = t;
i.J = e.U;
t.U = e.J;
e.U = i;
e.J = t;
if (i === this.X) {
this.X = e;
this.S.tt = e;
} else {
const t = i.tt;
if (t.U === i) {
t.U = e;
} else t.J = e;
}
e.tt = i.tt;
t.tt = e;
i.tt = e;
i.se = 1;
return {
parentNode: t,
grandParent: i,
curNode: e
};
} else {
t.se = 0;
if (i === this.X) {
this.X = i.rotateLeft();
} else i.rotateLeft();
i.se = 1;
}
}
return;
}
}
he(e, t, i) {
if (this.X === undefined) {
this.o += 1;
this.X = new this.ne(e, t);
this.X.se = 0;
this.X.tt = this.S;
this.S.tt = this.X;
this.S.U = this.X;
this.S.J = this.X;
return;
}
let s;
const r = this.S.U;
const n = this.p(r.T, e);
if (n === 0) {
r.L = t;
return;
} else if (n > 0) {
r.U = new this.ne(e, t);
r.U.tt = r;
s = r.U;
this.S.U = s;
} else {
const r = this.S.J;
const n = this.p(r.T, e);
if (n === 0) {
r.L = t;
return;
} else if (n < 0) {
r.J = new this.ne(e, t);
r.J.tt = r;
s = r.J;
this.S.J = s;
} else {
if (i !== undefined) {
const r = i.I;
if (r !== this.S) {
const i = this.p(r.T, e);
if (i === 0) {
r.L = t;
return;
} else if (i > 0) {
const i = r.pre();
const n = this.p(i.T, e);
if (n === 0) {
i.L = t;
return;
} else if (n < 0) {
s = new this.ne(e, t);
if (i.J === undefined) {
i.J = s;
s.tt = i;
} else {
r.U = s;
s.tt = r;
}
}
}
}
}
if (s === undefined) {
s = this.X;
while (true) {
const i = this.p(s.T, e);
if (i > 0) {
if (s.U === undefined) {
s.U = new this.ne(e, t);
s.U.tt = s;
s = s.U;
break;
}
s = s.U;
} else if (i < 0) {
if (s.J === undefined) {
s.J = new this.ne(e, t);
s.J.tt = s;
s = s.J;
break;
}
s = s.J;
} else {
s.L = t;
return;
}
}
}
}
}
this.o += 1;
return s;
}
clear() {
this.o = 0;
this.X = undefined;
this.S.tt = undefined;
this.S.U = this.S.J = undefined;
}
updateKeyByIterator(e, t) {
const i = e.I;
if (i === this.S) {
throw new TypeError("Invalid iterator!");
}
if (this.o === 1) {
i.T = t;
return true;
}
if (i === this.S.U) {
if (this.p(i.next().T, t) > 0) {
i.T = t;
return true;
}
return false;
}
if (i === this.S.J) {
if (this.p(i.pre().T, t) < 0) {
i.T = t;
return true;
}
return false;
}
const s = i.pre().T;
if (this.p(s, t) >= 0) return false;
const r = i.next().T;
if (this.p(r, t) <= 0) return false;
i.T = t;
return true;
}
eraseElementByPos(e) {
if (e < 0 || e > this.o - 1) {
throw new RangeError;
}
let t = 0;
this.ie(this.X, (i => {
if (e === t) {
this.ue(i);
return true;
}
t += 1;
return false;
}));
}
re(e, t) {
while (e) {
const i = this.p(e.T, t);
if (i < 0) {
e = e.J;
} else if (i > 0) {
e = e.U;
} else return e;
}
return e;
}
eraseElementByKey(e) {
if (!this.o) return;
const t = this.re(this.X, e);
if (t === undefined) return;
this.ue(t);
}
eraseElementByIterator(e) {
const t = e.I;
if (t === this.S) {
throw new RangeError("Invalid iterator");
}
if (t.J === undefined) {
e = e.next();
}
this.ue(t);
return e;
}
getHeight() {
if (!this.o) return 0;
const traversal = e => {
if (!e) return 0;
return Math.max(traversal(e.U), traversal(e.J)) + 1;
};
return traversal(this.X);
}
}
var _default = TreeContainer;
exports.default = _default;

View File

@@ -0,0 +1,42 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { initContainer } from "../ContainerBase";
export declare class OrderedMapIterator<K, V> extends TreeIterator<K, V> {
get pointer(): [K, V];
copy(): OrderedMapIterator<K, V>;
}
declare class OrderedMap<K, V> extends TreeContainer<K, V> {
/**
* @param container The initialization container.
* @param cmp The compare function.
* @param enableIndex Whether to enable iterator indexing function.
*/
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;
forEach(callback: (element: [K, V], index: number) => void): void;
lowerBound(_key: K): OrderedMapIterator<K, V>;
upperBound(_key: K): OrderedMapIterator<K, V>;
reverseLowerBound(_key: K): OrderedMapIterator<K, V>;
reverseUpperBound(_key: K): OrderedMapIterator<K, V>;
/**
* @description Insert a _key-_value pair or set _value by the given _key.
* @param _key The _key want to insert.
* @param _value The _value want to set.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
setElement(_key: K, _value: V, hint?: OrderedMapIterator<K, V>): void;
find(_key: K): OrderedMapIterator<K, V>;
/**
* @description Get the _value of the element of the specified _key.
*/
getElementByKey(_key: K): V | undefined;
getElementByPos(pos: number): [K, V];
union(other: OrderedMap<K, V>): void;
[Symbol.iterator](): Generator<[K, V], void, undefined>;
}
export default OrderedMap;

View File

@@ -0,0 +1,136 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = exports.OrderedMapIterator = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _TreeIterator = _interopRequireDefault(require("./Base/TreeIterator"));
function _interopRequireDefault(e) {
return e && e.t ? e : {
default: e
};
}
class OrderedMapIterator extends _TreeIterator.default {
get pointer() {
if (this.I === this.S) {
throw new RangeError("OrderedMap iterator access denied");
}
return new Proxy([], {
get: (e, r) => {
if (r === "0") return this.I.T; else if (r === "1") return this.I.L;
},
set: (e, r, t) => {
if (r !== "1") {
throw new TypeError("props must be 1");
}
this.I.L = t;
return true;
}
});
}
copy() {
return new OrderedMapIterator(this.I, this.S, this.iteratorType);
}
}
exports.OrderedMapIterator = OrderedMapIterator;
class OrderedMap extends _Base.default {
constructor(e = [], r, t) {
super(r, t);
this.K = function*(e) {
if (e === undefined) return;
yield* this.K(e.U);
yield [ e.T, e.L ];
yield* this.K(e.J);
};
e.forEach((([e, r]) => this.setElement(e, r)));
}
begin() {
return new OrderedMapIterator(this.S.U || this.S, this.S);
}
end() {
return new OrderedMapIterator(this.S, this.S);
}
rBegin() {
return new OrderedMapIterator(this.S.J || this.S, this.S, 1);
}
rEnd() {
return new OrderedMapIterator(this.S, this.S, 1);
}
front() {
if (!this.o) return undefined;
const e = this.S.U;
return [ e.T, e.L ];
}
back() {
if (!this.o) return undefined;
const e = this.S.J;
return [ e.T, e.L ];
}
forEach(e) {
let r = 0;
for (const t of this) e(t, r++);
}
lowerBound(e) {
const r = this.W(this.X, e);
return new OrderedMapIterator(r, this.S);
}
upperBound(e) {
const r = this.Y(this.X, e);
return new OrderedMapIterator(r, this.S);
}
reverseLowerBound(e) {
const r = this.Z(this.X, e);
return new OrderedMapIterator(r, this.S);
}
reverseUpperBound(e) {
const r = this.$(this.X, e);
return new OrderedMapIterator(r, this.S);
}
setElement(e, r, t) {
this.ee(e, r, t);
}
find(e) {
const r = this.re(this.X, e);
if (r !== undefined) {
return new OrderedMapIterator(r, this.S);
}
return this.end();
}
getElementByKey(e) {
const r = this.re(this.X, e);
return r ? r.L : undefined;
}
getElementByPos(e) {
if (e < 0 || e > this.o - 1) {
throw new RangeError;
}
let r;
let t = 0;
for (const s of this) {
if (t === e) {
r = s;
break;
}
t += 1;
}
return r;
}
union(e) {
e.forEach((([e, r]) => this.setElement(e, r)));
}
[Symbol.iterator]() {
return this.K(this.X);
}
}
var _default = OrderedMap;
exports.default = _default;

View File

@@ -0,0 +1,37 @@
import TreeContainer from './Base';
import TreeIterator from './Base/TreeIterator';
import { initContainer } from "../ContainerBase";
export declare class OrderedSetIterator<K> extends TreeIterator<K, undefined> {
get pointer(): K;
copy(): OrderedSetIterator<K>;
}
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.
*/
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;
forEach(callback: (element: K, index: number) => void): void;
getElementByPos(pos: number): K;
/**
* @description Insert element to set.
* @param _key The _key want to insert.
* @param hint You can give an iterator hint to improve insertion efficiency.
*/
insert(_key: K, hint?: OrderedSetIterator<K>): void;
find(element: K): OrderedSetIterator<K>;
lowerBound(_key: K): OrderedSetIterator<K>;
upperBound(_key: K): OrderedSetIterator<K>;
reverseLowerBound(_key: K): OrderedSetIterator<K>;
reverseUpperBound(_key: K): OrderedSetIterator<K>;
union(other: OrderedSet<K>): void;
[Symbol.iterator](): Generator<K, void, undefined>;
}
export default OrderedSet;

View File

@@ -0,0 +1,117 @@
"use strict";
Object.defineProperty(exports, "t", {
value: true
});
exports.default = exports.OrderedSetIterator = void 0;
var _Base = _interopRequireDefault(require("./Base"));
var _TreeIterator = _interopRequireDefault(require("./Base/TreeIterator"));
function _interopRequireDefault(e) {
return e && e.t ? e : {
default: e
};
}
class OrderedSetIterator extends _TreeIterator.default {
get pointer() {
if (this.I === this.S) {
throw new RangeError("OrderedSet iterator access denied!");
}
return this.I.T;
}
copy() {
return new OrderedSetIterator(this.I, this.S, this.iteratorType);
}
}
exports.OrderedSetIterator = OrderedSetIterator;
class OrderedSet extends _Base.default {
constructor(e = [], t, r) {
super(t, r);
this.K = function*(e) {
if (e === undefined) return;
yield* this.K(e.U);
yield e.T;
yield* this.K(e.J);
};
e.forEach((e => this.insert(e)));
}
begin() {
return new OrderedSetIterator(this.S.U || this.S, this.S);
}
end() {
return new OrderedSetIterator(this.S, this.S);
}
rBegin() {
return new OrderedSetIterator(this.S.J || this.S, this.S, 1);
}
rEnd() {
return new OrderedSetIterator(this.S, this.S, 1);
}
front() {
return this.S.U ? this.S.U.T : undefined;
}
back() {
return this.S.J ? this.S.J.T : undefined;
}
forEach(e) {
let t = 0;
for (const r of this) e(r, t++);
}
getElementByPos(e) {
if (e < 0 || e > this.o - 1) {
throw new RangeError;
}
let t;
let r = 0;
for (const i of this) {
if (r === e) {
t = i;
break;
}
r += 1;
}
return t;
}
insert(e, t) {
this.ee(e, undefined, t);
}
find(e) {
const t = this.re(this.X, e);
if (t !== undefined) {
return new OrderedSetIterator(t, this.S);
}
return this.end();
}
lowerBound(e) {
const t = this.W(this.X, e);
return new OrderedSetIterator(t, this.S);
}
upperBound(e) {
const t = this.Y(this.X, e);
return new OrderedSetIterator(t, this.S);
}
reverseLowerBound(e) {
const t = this.Z(this.X, e);
return new OrderedSetIterator(t, this.S);
}
reverseUpperBound(e) {
const t = this.$(this.X, e);
return new OrderedSetIterator(t, this.S);
}
union(e) {
e.forEach((e => this.insert(e)));
}
[Symbol.iterator]() {
return this.K(this.X);
}
}
var _default = OrderedSet;
exports.default = _default;