first commit

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

View File

@@ -0,0 +1,79 @@
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.
* @example
* new PriorityQueue();
* new PriorityQueue([1, 2, 3]);
* new PriorityQueue([1, 2, 3], (x, y) => x - y);
* new PriorityQueue([1, 2, 3], (x, y) => x - y, false);
*/
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.
* @returns The size of heap after pushing.
* @example
* queue.push(1);
*/
push(item: T): void;
/**
* @description Removes the top element.
* @returns The element you popped.
* @example
* queue.pop();
*/
pop(): T | undefined;
/**
* @description Accesses the top element.
* @example
* const top = queue.top();
*/
top(): T | undefined;
/**
* @description Check if element is in heap.
* @param item - The item want to find.
* @returns Whether element is in heap.
* @example
* const que = new PriorityQueue([], (x, y) => x.id - y.id);
* const obj = { id: 1 };
* que.push(obj);
* console.log(que.find(obj)); // true
*/
find(item: T): boolean;
/**
* @description Remove specified item from heap.
* @param item - The item want to remove.
* @returns Whether remove success.
* @example
* const que = new PriorityQueue([], (x, y) => x.id - y.id);
* const obj = { id: 1 };
* que.push(obj);
* que.remove(obj);
*/
remove(item: T): boolean;
/**
* @description Update item and it's pos in the heap.
* @param item - The item want to update.
* @returns Whether update success.
* @example
* const que = new PriorityQueue([], (x, y) => x.id - y.id);
* const obj = { id: 1 };
* que.push(obj);
* obj.id = 2;
* que.updateItem(obj);
*/
updateItem(item: T): boolean;
/**
* @returns Return a copy array of heap.
* @example
* const arr = queue.toArray();
*/
toArray(): T[];
}
export default PriorityQueue;

View File

@@ -0,0 +1,171 @@
var __extends = this && this.t || function() {
var extendStatics = function(i, r) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(i, r) {
i.__proto__ = r;
} || function(i, r) {
for (var t in r) if (Object.prototype.hasOwnProperty.call(r, t)) i[t] = r[t];
};
return extendStatics(i, r);
};
return function(i, r) {
if (typeof r !== "function" && r !== null) throw new TypeError("Class extends value " + String(r) + " is not a constructor or null");
extendStatics(i, r);
function __() {
this.constructor = i;
}
i.prototype = r === null ? Object.create(r) : (__.prototype = r.prototype, new __);
};
}();
var __read = this && this.q || function(i, r) {
var t = typeof Symbol === "function" && i[Symbol.iterator];
if (!t) return i;
var e = t.call(i), n, u = [], s;
try {
while ((r === void 0 || r-- > 0) && !(n = e.next()).done) u.push(n.value);
} catch (i) {
s = {
error: i
};
} finally {
try {
if (n && !n.done && (t = e["return"])) t.call(e);
} finally {
if (s) throw s.error;
}
}
return u;
};
var __spreadArray = this && this.D || function(i, r, t) {
if (t || arguments.length === 2) for (var e = 0, n = r.length, u; e < n; e++) {
if (u || !(e in r)) {
if (!u) u = Array.prototype.slice.call(r, 0, e);
u[e] = r[e];
}
}
return i.concat(u || Array.prototype.slice.call(r));
};
import { Base } from "../ContainerBase";
var PriorityQueue = function(i) {
__extends(PriorityQueue, i);
function PriorityQueue(r, t, e) {
if (r === void 0) {
r = [];
}
if (t === void 0) {
t = function(i, r) {
if (i > r) return -1;
if (i < r) return 1;
return 0;
};
}
if (e === void 0) {
e = true;
}
var n = i.call(this) || this;
n.$ = t;
if (Array.isArray(r)) {
n.ii = e ? __spreadArray([], __read(r), false) : r;
} else {
n.ii = [];
var u = n;
r.forEach((function(i) {
u.ii.push(i);
}));
}
n.M = n.ii.length;
var s = n.M >> 1;
for (var o = n.M - 1 >> 1; o >= 0; --o) {
n.ri(o, s);
}
return n;
}
PriorityQueue.prototype.ti = function(i) {
var r = this.ii[i];
while (i > 0) {
var t = i - 1 >> 1;
var e = this.ii[t];
if (this.$(e, r) <= 0) break;
this.ii[i] = e;
i = t;
}
this.ii[i] = r;
};
PriorityQueue.prototype.ri = function(i, r) {
var t = this.ii[i];
while (i < r) {
var e = i << 1 | 1;
var n = e + 1;
var u = this.ii[e];
if (n < this.M && this.$(u, this.ii[n]) > 0) {
e = n;
u = this.ii[n];
}
if (this.$(u, t) >= 0) break;
this.ii[i] = u;
i = e;
}
this.ii[i] = t;
};
PriorityQueue.prototype.clear = function() {
this.M = 0;
this.ii.length = 0;
};
PriorityQueue.prototype.push = function(i) {
this.ii.push(i);
this.ti(this.M);
this.M += 1;
};
PriorityQueue.prototype.pop = function() {
if (this.M === 0) return;
var i = this.ii[0];
var r = this.ii.pop();
this.M -= 1;
if (this.M) {
this.ii[0] = r;
this.ri(0, this.M >> 1);
}
return i;
};
PriorityQueue.prototype.top = function() {
return this.ii[0];
};
PriorityQueue.prototype.find = function(i) {
return this.ii.indexOf(i) >= 0;
};
PriorityQueue.prototype.remove = function(i) {
var r = this.ii.indexOf(i);
if (r < 0) return false;
if (r === 0) {
this.pop();
} else if (r === this.M - 1) {
this.ii.pop();
this.M -= 1;
} else {
this.ii.splice(r, 1, this.ii.pop());
this.M -= 1;
this.ti(r);
this.ri(r, this.M >> 1);
}
return true;
};
PriorityQueue.prototype.updateItem = function(i) {
var r = this.ii.indexOf(i);
if (r < 0) return false;
this.ti(r);
this.ri(r, this.M >> 1);
return true;
};
PriorityQueue.prototype.toArray = function() {
return __spreadArray([], __read(this.ii), false);
};
return PriorityQueue;
}(Base);
export default PriorityQueue;
//# sourceMappingURL=PriorityQueue.js.map

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -0,0 +1,69 @@
var __extends = this && this.t || function() {
var extendStatics = function(t, i) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(t, i) {
t.__proto__ = i;
} || function(t, i) {
for (var n in i) if (Object.prototype.hasOwnProperty.call(i, n)) t[n] = i[n];
};
return extendStatics(t, i);
};
return function(t, i) {
if (typeof i !== "function" && i !== null) throw new TypeError("Class extends value " + String(i) + " is not a constructor or null");
extendStatics(t, i);
function __() {
this.constructor = t;
}
t.prototype = i === null ? Object.create(i) : (__.prototype = i.prototype, new __);
};
}();
import { Base } from "../ContainerBase";
var Queue = function(t) {
__extends(Queue, t);
function Queue(i) {
if (i === void 0) {
i = [];
}
var n = t.call(this) || this;
n.A = 0;
n.tt = [];
var e = n;
i.forEach((function(t) {
e.push(t);
}));
return n;
}
Queue.prototype.clear = function() {
this.tt = [];
this.M = this.A = 0;
};
Queue.prototype.push = function(t) {
var i = this.tt.length;
if (this.A / i > .5 && this.A + this.M >= i && i > 4096) {
var n = this.M;
for (var e = 0; e < n; ++e) {
this.tt[e] = this.tt[this.A + e];
}
this.A = 0;
this.tt[this.M] = t;
} else this.tt[this.A + this.M] = t;
return ++this.M;
};
Queue.prototype.pop = function() {
if (this.M === 0) return;
var t = this.tt[this.A++];
this.M -= 1;
return t;
};
Queue.prototype.front = function() {
if (this.M === 0) return;
return this.tt[this.A];
};
return Queue;
}(Base);
export default Queue;
//# sourceMappingURL=Queue.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,22 @@
import { Base, initContainer } from "../ContainerBase";
declare class Stack<T> extends Base {
constructor(container?: initContainer<T>);
clear(): void;
/**
* @description Insert element to stack's end.
* @description The element you want to push to the back.
* @returns The container length after erasing.
*/
push(element: T): number;
/**
* @description Removes the end element.
* @returns The element you popped.
*/
pop(): T | undefined;
/**
* @description Accesses the end element.
* @returns The last element.
*/
top(): T | undefined;
}
export default Stack;

View File

@@ -0,0 +1,59 @@
var __extends = this && this.t || function() {
var extendStatics = function(t, n) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(t, n) {
t.__proto__ = n;
} || function(t, n) {
for (var i in n) if (Object.prototype.hasOwnProperty.call(n, i)) t[i] = n[i];
};
return extendStatics(t, n);
};
return function(t, n) {
if (typeof n !== "function" && n !== null) throw new TypeError("Class extends value " + String(n) + " is not a constructor or null");
extendStatics(t, n);
function __() {
this.constructor = t;
}
t.prototype = n === null ? Object.create(n) : (__.prototype = n.prototype, new __);
};
}();
import { Base } from "../ContainerBase";
var Stack = function(t) {
__extends(Stack, t);
function Stack(n) {
if (n === void 0) {
n = [];
}
var i = t.call(this) || this;
i.nt = [];
var r = i;
n.forEach((function(t) {
r.push(t);
}));
return i;
}
Stack.prototype.clear = function() {
this.M = 0;
this.nt = [];
};
Stack.prototype.push = function(t) {
this.nt.push(t);
this.M += 1;
return this.M;
};
Stack.prototype.pop = function() {
if (this.M === 0) return;
this.M -= 1;
return this.nt.pop();
};
Stack.prototype.top = function() {
return this.nt[this.M - 1];
};
return Stack;
}(Base);
export default Stack;
//# sourceMappingURL=Stack.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["container/OtherContainer/Stack.js","../../src/container/OtherContainer/Stack.ts"],"names":["__extends","this","extendStatics","d","b","Object","setPrototypeOf","__proto__","Array","p","prototype","hasOwnProperty","call","TypeError","String","__","constructor","create","Base","Stack","_super","container","_this","_stack","self","forEach","el","push","clear","_length","element","pop","top"],"mappings":"AAAA,IAAIA,YAAaC,QAAQA,KAAKD,KAAe;IACzC,IAAIE,gBAAgB,SAAUC,GAAGC;QAC7BF,gBAAgBG,OAAOC,kBAClB;YAAEC,WAAW;qBAAgBC,SAAS,SAAUL,GAAGC;YAAKD,EAAEI,YAAYH;AAAG,aAC1E,SAAUD,GAAGC;YAAK,KAAK,IAAIK,KAAKL,GAAG,IAAIC,OAAOK,UAAUC,eAAeC,KAAKR,GAAGK,IAAIN,EAAEM,KAAKL,EAAEK;AAAI;QACpG,OAAOP,cAAcC,GAAGC;AAC5B;IACA,OAAO,SAAUD,GAAGC;QAChB,WAAWA,MAAM,cAAcA,MAAM,MACjC,MAAM,IAAIS,UAAU,yBAAyBC,OAAOV,KAAK;QAC7DF,cAAcC,GAAGC;QACjB,SAASW;YAAOd,KAAKe,cAAcb;AAAG;QACtCA,EAAEO,YAAYN,MAAM,OAAOC,OAAOY,OAAOb,MAAMW,GAAGL,YAAYN,EAAEM,WAAW,IAAIK;AACnF;AACJ,CAd6C;;SCApCG,YAAqB;;AAE9B,IAAAC,QAAA,SAAAC;IAAuBpB,UAAAmB,OAAAC;IAKrB,SAAAD,MAAYE;QAAA,IAAAA,WAAA,GAAA;YAAAA,IAAA;AAAgC;QAA5C,IAAAC,IACEF,EAAAR,KAAAX,SAAOA;QAFDqB,EAAAC,KAAc;QAGpB,IAAMC,IAAOF;QACbD,EAAUI,SAAQ,SAAUC;YAC1BF,EAAKG,KAAKD;ADiBR;QACA,OAAOJ;AACX;IChBFH,MAAAT,UAAAkB,QAAA;QACE3B,KAAK4B,IAAU;QACf5B,KAAKsB,KAAS;ADkBd;ICXFJ,MAAAT,UAAAiB,OAAA,SAAKG;QACH7B,KAAKsB,GAAOI,KAAKG;QACjB7B,KAAK4B,KAAW;QAChB,OAAO5B,KAAK4B;ADkBZ;ICZFV,MAAAT,UAAAqB,MAAA;QACE,IAAI9B,KAAK4B,MAAY,GAAG;QACxB5B,KAAK4B,KAAW;QAChB,OAAO5B,KAAKsB,GAAOQ;ADmBnB;ICbFZ,MAAAT,UAAAsB,MAAA;QACE,OAAO/B,KAAKsB,GAAOtB,KAAK4B,IAAU;ADmBlC;ICjBJ,OAAAV;AAAA,CA1CA,CAAuBD;;eA4CRC","file":"Stack.js","sourcesContent":["var __extends = (this && this.__extends) || (function () {\n var extendStatics = function (d, b) {\n extendStatics = Object.setPrototypeOf ||\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\n return extendStatics(d, b);\n };\n return function (d, b) {\n if (typeof b !== \"function\" && b !== null)\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\n extendStatics(d, b);\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n };\n})();\nimport { Base } from \"../ContainerBase\";\nvar Stack = /** @class */ (function (_super) {\n __extends(Stack, _super);\n function Stack(container) {\n if (container === void 0) { container = []; }\n var _this = _super.call(this) || this;\n /**\n * @internal\n */\n _this._stack = [];\n var self = _this;\n container.forEach(function (el) {\n self.push(el);\n });\n return _this;\n }\n Stack.prototype.clear = function () {\n this._length = 0;\n this._stack = [];\n };\n /**\n * @description Insert element to stack's end.\n * @description The element you want to push to the back.\n * @returns The container length after erasing.\n */\n Stack.prototype.push = function (element) {\n this._stack.push(element);\n this._length += 1;\n return this._length;\n };\n /**\n * @description Removes the end element.\n * @returns The element you popped.\n */\n Stack.prototype.pop = function () {\n if (this._length === 0)\n return;\n this._length -= 1;\n return this._stack.pop();\n };\n /**\n * @description Accesses the end element.\n * @returns The last element.\n */\n Stack.prototype.top = function () {\n return this._stack[this._length - 1];\n };\n return Stack;\n}(Base));\nexport default Stack;\n","import { Base, initContainer } from '@/container/ContainerBase';\n\nclass Stack<T> extends Base {\n /**\n * @internal\n */\n private _stack: T[] = [];\n constructor(container: initContainer<T> = []) {\n super();\n const self = this;\n container.forEach(function (el) {\n self.push(el);\n });\n }\n clear() {\n this._length = 0;\n this._stack = [];\n }\n /**\n * @description Insert element to stack's end.\n * @description The element you want to push to the back.\n * @returns The container length after erasing.\n */\n push(element: T) {\n this._stack.push(element);\n this._length += 1;\n return this._length;\n }\n /**\n * @description Removes the end element.\n * @returns The element you popped.\n */\n pop() {\n if (this._length === 0) return;\n this._length -= 1;\n return this._stack.pop();\n }\n /**\n * @description Accesses the end element.\n * @returns The last element.\n */\n top(): T | undefined {\n return this._stack[this._length - 1];\n }\n}\n\nexport default Stack;\n"]}