Добавлена ДБ

This commit is contained in:
Georgiy Syralev
2025-09-15 18:10:26 +03:00
parent 253ad8c19b
commit c954b5268e
5824 changed files with 1107594 additions and 19141 deletions

View File

@@ -0,0 +1,192 @@
import * as Arr from "../../Array.js";
import * as Option from "../../Option.js";
/** @internal */
export const Direction = {
Forward: 0,
Backward: 1 << 0
};
/** @internal */
export class RedBlackTreeIterator {
self;
stack;
direction;
count = 0;
constructor(self, stack, direction) {
this.self = self;
this.stack = stack;
this.direction = direction;
}
/**
* Clones the iterator
*/
clone() {
return new RedBlackTreeIterator(this.self, this.stack.slice(), this.direction);
}
/**
* Reverse the traversal direction
*/
reversed() {
return new RedBlackTreeIterator(this.self, this.stack.slice(), this.direction === Direction.Forward ? Direction.Backward : Direction.Forward);
}
/**
* Iterator next
*/
next() {
const entry = this.entry;
this.count++;
if (this.direction === Direction.Forward) {
this.moveNext();
} else {
this.movePrev();
}
switch (entry._tag) {
case "None":
{
return {
done: true,
value: this.count
};
}
case "Some":
{
return {
done: false,
value: entry.value
};
}
}
}
/**
* Returns the key
*/
get key() {
if (this.stack.length > 0) {
return Option.some(this.stack[this.stack.length - 1].key);
}
return Option.none();
}
/**
* Returns the value
*/
get value() {
if (this.stack.length > 0) {
return Option.some(this.stack[this.stack.length - 1].value);
}
return Option.none();
}
/**
* Returns the key
*/
get entry() {
return Option.map(Arr.last(this.stack), node => [node.key, node.value]);
}
/**
* Returns the position of this iterator in the sorted list
*/
get index() {
let idx = 0;
const stack = this.stack;
if (stack.length === 0) {
const r = this.self._root;
if (r != null) {
return r.count;
}
return 0;
} else if (stack[stack.length - 1].left != null) {
idx = stack[stack.length - 1].left.count;
}
for (let s = stack.length - 2; s >= 0; --s) {
if (stack[s + 1] === stack[s].right) {
;
++idx;
if (stack[s].left != null) {
idx += stack[s].left.count;
}
}
}
return idx;
}
/**
* Advances iterator to next element in list
*/
moveNext() {
const stack = this.stack;
if (stack.length === 0) {
return;
}
let n = stack[stack.length - 1];
if (n.right != null) {
n = n.right;
while (n != null) {
stack.push(n);
n = n.left;
}
} else {
stack.pop();
while (stack.length > 0 && stack[stack.length - 1].right === n) {
n = stack[stack.length - 1];
stack.pop();
}
}
}
/**
* Checks if there is a next element
*/
get hasNext() {
const stack = this.stack;
if (stack.length === 0) {
return false;
}
if (stack[stack.length - 1].right != null) {
return true;
}
for (let s = stack.length - 1; s > 0; --s) {
if (stack[s - 1].left === stack[s]) {
return true;
}
}
return false;
}
/**
* Advances iterator to previous element in list
*/
movePrev() {
const stack = this.stack;
if (stack.length === 0) {
return;
}
let n = stack[stack.length - 1];
if (n != null && n.left != null) {
n = n.left;
while (n != null) {
stack.push(n);
n = n.right;
}
} else {
stack.pop();
while (stack.length > 0 && stack[stack.length - 1].left === n) {
n = stack[stack.length - 1];
stack.pop();
}
}
}
/**
* Checks if there is a previous element
*/
get hasPrev() {
const stack = this.stack;
if (stack.length === 0) {
return false;
}
if (stack[stack.length - 1].left != null) {
return true;
}
for (let s = stack.length - 1; s > 0; --s) {
if (stack[s - 1].right === stack[s]) {
return true;
}
}
return false;
}
}
//# sourceMappingURL=iterator.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,50 @@
/** @internal */
export const Color = {
Red: 0,
Black: 1 << 0
};
/** @internal */
export const clone = ({
color,
count,
key,
left,
right,
value
}) => ({
color,
key,
value,
left,
right,
count
});
/** @internal */
export function swap(n, v) {
n.key = v.key;
n.value = v.value;
n.left = v.left;
n.right = v.right;
n.color = v.color;
n.count = v.count;
}
/** @internal */
export const repaint = ({
count,
key,
left,
right,
value
}, color) => ({
color,
key,
value,
left,
right,
count
});
/** @internal */
export const recount = node => {
node.count = 1 + (node.left?.count ?? 0) + (node.right?.count ?? 0);
};
//# sourceMappingURL=node.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"node.js","names":["Color","Red","Black","clone","color","count","key","left","right","value","swap","n","v","repaint","recount","node"],"sources":["../../../../src/internal/redBlackTree/node.ts"],"sourcesContent":[null],"mappings":"AAAA;AACA,OAAO,MAAMA,KAAK,GAAG;EACnBC,GAAG,EAAE,CAAe;EACpBC,KAAK,EAAE,CAAC,IAAI;CACJ;AAiBV;AACA,OAAO,MAAMC,KAAK,GAAGA,CAAO;EAC1BC,KAAK;EACLC,KAAK;EACLC,GAAG;EACHC,IAAI;EACJC,KAAK;EACLC;AAAK,CACM,MAAM;EACjBL,KAAK;EACLE,GAAG;EACHG,KAAK;EACLF,IAAI;EACJC,KAAK;EACLH;CACD,CAAC;AAEF;AACA,OAAM,SAAUK,IAAIA,CAAOC,CAAa,EAAEC,CAAa;EACrDD,CAAC,CAACL,GAAG,GAAGM,CAAC,CAACN,GAAG;EACbK,CAAC,CAACF,KAAK,GAAGG,CAAC,CAACH,KAAK;EACjBE,CAAC,CAACJ,IAAI,GAAGK,CAAC,CAACL,IAAI;EACfI,CAAC,CAACH,KAAK,GAAGI,CAAC,CAACJ,KAAK;EACjBG,CAAC,CAACP,KAAK,GAAGQ,CAAC,CAACR,KAAK;EACjBO,CAAC,CAACN,KAAK,GAAGO,CAAC,CAACP,KAAK;AACnB;AAEA;AACA,OAAO,MAAMQ,OAAO,GAAGA,CAAO;EAC5BR,KAAK;EACLC,GAAG;EACHC,IAAI;EACJC,KAAK;EACLC;AAAK,CACM,EAAEL,KAAiB,MAAM;EACpCA,KAAK;EACLE,GAAG;EACHG,KAAK;EACLF,IAAI;EACJC,KAAK;EACLH;CACD,CAAC;AAEF;AACA,OAAO,MAAMS,OAAO,GAAUC,IAAgB,IAAI;EAChDA,IAAI,CAACV,KAAK,GAAG,CAAC,IAAIU,IAAI,CAACR,IAAI,EAAEF,KAAK,IAAI,CAAC,CAAC,IAAIU,IAAI,CAACP,KAAK,EAAEH,KAAK,IAAI,CAAC,CAAC;AACrE,CAAC","ignoreList":[]}