all these changes

This commit is contained in:
Jake Kasper
2026-04-09 13:19:47 -05:00
parent e83a51a051
commit 65315f36d1
39102 changed files with 7932979 additions and 567 deletions

222
frontend/node_modules/splaytree/Readme.md generated vendored Normal file
View File

@@ -0,0 +1,222 @@
# Fast splay tree [![npm version](https://badge.fury.io/js/splaytree.svg)](https://badge.fury.io/js/splaytree) [![codecov](https://codecov.io/gh/w8r/splay-tree/branch/master/graph/badge.svg)](https://codecov.io/gh/w8r/splay-tree)
[Splay-tree](https://en.wikipedia.org/wiki/Splay_tree): **[fast](#benchmarks)**(non-recursive) and **simple**(< 1000 lines of code)
Implementation is adapted directly from Wikipedia with the same API as [w8r/avl](https://github.com/w8r/avl), to run the benchmarks against other trees.
This tree is based on **top-down** splaying algorithm by D.Sleator. It supports
- splitting, merging
- updating of the keys
- bulk loading of the items into an empty or non-empty tree
- insertion with duplicates or no duplicates
- lookup without splaying
![Splay-tree](https://i.stack.imgur.com/CNSAZ.png)
| Operation | Average | Worst case |
| --------- | ------------ | ---------------------- |
| Space | **O(n)** | **O(n)** |
| Search | **O(log n)** | **amortized O(log n)** |
| Insert | **O(log n)** | **amortized O(log n)** |
| Delete | **O(log n)** | **amortized O(log n)** |
## Install
```shell
npm i -S splaytree
```
```js
import SplayTree from "splaytree";
const tree = new SplayTree();
```
Or get it from CDN
```html
<script src="https://unpkg.com/splaytree"></script>
<script>
var tree = new SplayTree();
...
</script>
```
Or use the compiled version 'dist/splaytree.umd.cjs'.
[Try it in your browser](https://npm.runkit.com/splaytree)
## API
- `new SplayTree([comparator])`, where `comparator` is optional comparison function
- `tree.insert(key:any, [data:any]):Node` - Insert item, allow duplicate keys
- `tree.add(key:any, [data:any]):Node` - Insert item if it is not present
- `tree.remove(key:any)` - Remove item
- `tree.find(key):Node|Null` - Return node by its key
- `tree.findStatic(key):Node|Null` - Return node by its key (doesn't re-balance the tree)
- `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
- `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
- `tree.forEach(function(node) {...}):Tree` In-order traversal
- `tree.keys():Array<key>` - Returns the array of keys in order
- `tree.values():Array<*>` - Returns the array of data fields in order
- `tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
- `tree.pop():Node` - Removes smallest node
- `tree.min():key` - Returns min key
- `tree.max():key` - Returns max key
- `tree.minNode():Node` - Returns the node with smallest key
- `tree.maxNode():Node` - Returns the node with highest key
- `tree.prev(node):Node` - Predecessor node
- `tree.next(node):Node` - Successor node
- `tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
**Comparator**
`function(a:key,b:key):Number` - Comparator function between two keys, it returns
- `0` if the keys are equal
- `<0` if `a < b`
- `>0` if `a > b`
The comparator function is extremely important, in case of errors you might end
up with a wrongly constructed tree or would not be able to retrieve your items.
It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
to make sure it's working correctly, otherwise you may have bugs that are very
unpredictable and hard to catch.
**Duplicate keys**
- `insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
points in 2D).
- `add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
## Example
```js
import Tree from "splaytree";
const t = new Tree();
t.insert(5);
t.insert(-10);
t.insert(0);
t.insert(33);
t.insert(2);
console.log(t.keys()); // [-10, 0, 2, 5, 33]
console.log(t.size); // 5
console.log(t.min()); // -10
console.log(t.max()); // -33
t.remove(0);
console.log(t.size); // 4
```
**Custom comparator (reverse sort)**
```js
import Tree from "splaytree";
const t = new Tree((a, b) => b - a);
t.insert(5);
t.insert(-10);
t.insert(0);
t.insert(33);
t.insert(2);
console.log(t.keys()); // [33, 5, 2, 0, -10]
```
**Bulk insert**
```js
import Tree from "splaytree";
const t = new Tree();
t.load([3, 2, -10, 20], ["C", "B", "A", "D"]);
console.log(t.keys()); // [-10, 2, 3, 20]
console.log(t.values()); // ['A', 'B', 'C', 'D']
```
## Benchmarks
```shell
npm run benchmark
```
```
Insert (x1000)
- Bintrees RB x 5,779 ops/sec ±1.37% (85 runs sampled) mean 0.173ms
- Splay (current) x 9,264 ops/sec ±2.70% (88 runs sampled) mean 0.108ms
- AVL x 7,459 ops/sec ±1.07% (91 runs sampled) mean 0.134ms
- Fastest is Splay (current)
Random read (x1000)
- Bintrees RB x 19,317 ops/sec ±0.52% (90 runs sampled) mean 0.052ms
- Splay (current) x 7,635 ops/sec ±0.92% (91 runs sampled) mean 0.131ms
- Splay (current) - static x 16,350 ops/sec ±0.75% (87 runs sampled) mean 0.061ms
- AVL x 15,782 ops/sec ±0.57% (91 runs sampled) mean 0.063ms
- Fastest is Bintrees RB
Remove (x1000)
- Bintrees RB x 195,282 ops/sec ±0.85% (90 runs sampled) mean 0.005ms
- Splay (current) x 364,630 ops/sec ±6.05% (87 runs sampled) mean 0.003ms
- AVL x 95,946 ops/sec ±0.76% (93 runs sampled) mean 0.010ms
- Fastest is Splay (current)
Bulk-load (x10000)
- 1 by 1 x 266 ops/sec ±1.58% (83 runs sampled) mean 3.755ms
- bulk load (build) x 287 ops/sec ±3.02% (76 runs sampled) mean 3.487ms
- Fastest is bulk load (build)
Bulk-add (x1000) to 1000
- 1 by 1 x 2,859 ops/sec ±3.35% (77 runs sampled) mean 0.350ms
- bulk add (rebuild) x 3,755 ops/sec ±2.67% (67 runs sampled) mean 0.266ms
- Fastest is bulk add (rebuild)
Bulk-remove-insert (10%) of 10000
- 1 by 1 x 771 ops/sec ±4.31% (68 runs sampled) mean 1.297ms
- bulk add (rebuild) x 706 ops/sec ±1.12% (88 runs sampled) mean 1.416ms
- Fastest is 1 by 1
Bulk-update (10%) of 10000
- 1 by 1 x 717 ops/sec ±1.96% (79 runs sampled) mean 1.394ms
- split-merge x 705 ops/sec ±1.68% (85 runs sampled) mean 1.419ms
- Fastest is 1 by 1
```
Adding google closure library to the benchmark is, of course, unfair, cause the
node.js version of it is not optimized by the compiler, but in this case it
plays the role of straight-forward robust implementation.
## Develop
```shell
npm i
npm t
npm run build
```
## TODO
- [ ] try and add parent fields for efficient `.prev()` and `.next()`, or iterators
## License
The MIT License (MIT)
Copyright (c) 2019 Alexander Milevski <info@w8r.name>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

82
frontend/node_modules/splaytree/dist/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,82 @@
import { default as Node } from './node';
import { Key } from './types';
export type Comparator<Key> = (a: Key, b: Key) => number;
export type Visitor<Key, Value> = (node: Node<Key, Value>) => undefined | boolean | void;
export type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;
declare function DEFAULT_COMPARE(a: Key, b: Key): number;
export default class Tree<Key = number, Value = any> {
private _comparator;
private _root;
private _size;
constructor(comparator?: typeof DEFAULT_COMPARE);
/**
* Inserts a key, allows duplicates
*/
insert(key: Key, data?: Value): Node<Key, Value>;
/**
* Adds a key, if it is not present in the tree
*/
add(key: Key, data?: Value): Node<Key, Value>;
/**
* @param {Key} key
* @return {Node|null}
*/
remove(key: Key): void;
/**
* Deletes i from the tree if it's there
*/
private _remove;
/**
* Removes and returns the node with smallest key
*/
pop(): {
key: Key;
data: Value;
} | null;
/**
* Find without splaying
*/
findStatic(key: Key): Node<Key, Value> | null;
find(key: Key): Node<Key, Value> | null;
contains(key: Key): boolean;
forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
*/
range(low: Key, high: Key, fn: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
/**
* Returns array of keys
*/
keys(): Key[];
/**
* Returns array of all the data in the nodes
*/
values(): Value[];
min(): Key | null;
max(): Key | null;
minNode(t?: Node<Key, Value> | null): Node<Key, Value> | null;
maxNode(t?: Node<Key, Value> | null): Node<Key, Value> | null;
/**
* Returns node at given index
*/
at(index: number): Node<Key, Value> | null;
next(d: Node<Key, Value>): Node<Key, Value> | null;
prev(d: Node<Key, Value>): Node<Key, Value> | null;
clear(): Tree<Key, Value>;
toList(): Node<any, any>;
/**
* Bulk-load items. Both array have to be same size
*/
load(keys: Key[], values?: Value[], presort?: boolean): this;
isEmpty(): boolean;
get size(): number;
get root(): Node<Key, Value> | null;
toString(printNode?: NodePrinter<Key, Value>): string;
update(key: Key, newKey: Key, newData?: Value): void;
split(key: Key): {
left: Node<import('./types').Key, import('./types').Value> | null;
right: Node<import('./types').Key, import('./types').Value> | null;
};
[Symbol.iterator](): Generator<Node<Key, Value>, void, unknown>;
}
export {};

8
frontend/node_modules/splaytree/dist/node.d.ts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
export default class Node<Key, Value> {
key: Key;
data: any;
left: Node<Key, Value> | null;
right: Node<Key, Value> | null;
next: Node<Key, Value> | null;
constructor(key: Key, data?: any);
}

344
frontend/node_modules/splaytree/dist/splaytree.js generated vendored Normal file
View File

@@ -0,0 +1,344 @@
class f {
constructor(t, e) {
this.next = null, this.key = t, this.data = e, this.left = null, this.right = null;
}
}
function d(n, t) {
return n > t ? 1 : n < t ? -1 : 0;
}
function u(n, t, e) {
const r = new f(null, null);
let l = r, i = r;
for (; ; ) {
const o = e(n, t.key);
if (o < 0) {
if (t.left === null) break;
if (e(n, t.left.key) < 0) {
const s = t.left;
if (t.left = s.right, s.right = t, t = s, t.left === null) break;
}
i.left = t, i = t, t = t.left;
} else if (o > 0) {
if (t.right === null) break;
if (e(n, t.right.key) > 0) {
const s = t.right;
if (t.right = s.left, s.left = t, t = s, t.right === null) break;
}
l.right = t, l = t, t = t.right;
} else break;
}
return l.right = t.left, i.left = t.right, t.left = r.right, t.right = r.left, t;
}
function c(n, t, e, r) {
const l = new f(n, t);
if (e === null)
return l.left = l.right = null, l;
e = u(n, e, r);
const i = r(n, e.key);
return i < 0 ? (l.left = e.left, l.right = e, e.left = null) : i >= 0 && (l.right = e.right, l.left = e, e.right = null), l;
}
function m(n, t, e) {
let r = null, l = null;
if (t) {
t = u(n, t, e);
const i = e(t.key, n);
i === 0 ? (r = t.left, l = t.right) : i < 0 ? (l = t.right, t.right = null, r = t) : (r = t.left, t.left = null, l = t);
}
return { left: r, right: l };
}
function w(n, t, e) {
return t === null ? n : (n === null || (t = u(n.key, t, e), t.left = n), t);
}
function _(n, t, e, r, l) {
if (n) {
r(`${t}${e ? "└── " : "├── "}${l(n)}
`);
const i = t + (e ? " " : "│ ");
n.left && _(n.left, i, !1, r, l), n.right && _(n.right, i, !0, r, l);
}
}
class z {
constructor(t = d) {
this._root = null, this._size = 0, this._comparator = t;
}
/**
* Inserts a key, allows duplicates
*/
insert(t, e) {
return this._size++, this._root = c(t, e, this._root, this._comparator);
}
/**
* Adds a key, if it is not present in the tree
*/
add(t, e) {
const r = new f(t, e);
this._root === null && (r.left = r.right = null, this._size++, this._root = r);
const l = this._comparator, i = u(t, this._root, l), o = l(t, i.key);
return o === 0 ? this._root = i : (o < 0 ? (r.left = i.left, r.right = i, i.left = null) : o > 0 && (r.right = i.right, r.left = i, i.right = null), this._size++, this._root = r), this._root;
}
/**
* @param {Key} key
* @return {Node|null}
*/
remove(t) {
this._root = this._remove(t, this._root, this._comparator);
}
/**
* Deletes i from the tree if it's there
*/
_remove(t, e, r) {
let l;
return e === null ? null : (e = u(t, e, r), r(t, e.key) === 0 ? (e.left === null ? l = e.right : (l = u(t, e.left, r), l.right = e.right), this._size--, l) : e);
}
/**
* Removes and returns the node with smallest key
*/
pop() {
let t = this._root;
if (t) {
for (; t.left; ) t = t.left;
return this._root = u(t.key, this._root, this._comparator), this._root = this._remove(t.key, this._root, this._comparator), { key: t.key, data: t.data };
}
return null;
}
/**
* Find without splaying
*/
findStatic(t) {
let e = this._root;
const r = this._comparator;
for (; e; ) {
const l = r(t, e.key);
if (l === 0) return e;
l < 0 ? e = e.left : e = e.right;
}
return null;
}
find(t) {
return this._root && (this._root = u(t, this._root, this._comparator), this._comparator(t, this._root.key) !== 0) ? null : this._root;
}
contains(t) {
let e = this._root;
const r = this._comparator;
for (; e; ) {
const l = r(t, e.key);
if (l === 0) return !0;
l < 0 ? e = e.left : e = e.right;
}
return !1;
}
forEach(t, e) {
let r = this._root;
const l = [];
let i = !1;
for (; !i; )
r !== null ? (l.push(r), r = r.left) : l.length !== 0 ? (r = l.pop(), t.call(e, r), r = r.right) : i = !0;
return this;
}
/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
*/
range(t, e, r, l) {
const i = [], o = this._comparator;
let s = this._root, h;
for (; i.length !== 0 || s; )
if (s)
i.push(s), s = s.left;
else {
if (s = i.pop(), h = o(s.key, e), h > 0)
break;
if (o(s.key, t) >= 0 && r.call(l, s))
return this;
s = s.right;
}
return this;
}
/**
* Returns array of keys
*/
keys() {
const t = [];
return this.forEach(({ key: e }) => {
t.push(e);
}), t;
}
/**
* Returns array of all the data in the nodes
*/
values() {
const t = [];
return this.forEach(({ data: e }) => {
t.push(e);
}), t;
}
min() {
return this._root ? this.minNode(this._root).key : null;
}
max() {
return this._root ? this.maxNode(this._root).key : null;
}
minNode(t = this._root) {
if (t) for (; t.left; ) t = t.left;
return t;
}
maxNode(t = this._root) {
if (t) for (; t.right; ) t = t.right;
return t;
}
/**
* Returns node at given index
*/
at(t) {
let e = this._root, r = !1, l = 0;
const i = [];
for (; !r; )
if (e)
i.push(e), e = e.left;
else if (i.length > 0) {
if (e = i.pop(), l === t) return e;
l++, e = e.right;
} else r = !0;
return null;
}
next(t) {
let e = this._root, r = null;
if (t.right) {
for (r = t.right; r.left; ) r = r.left;
return r;
}
const l = this._comparator;
for (; e; ) {
const i = l(t.key, e.key);
if (i === 0) break;
i < 0 ? (r = e, e = e.left) : e = e.right;
}
return r;
}
prev(t) {
let e = this._root, r = null;
if (t.left !== null) {
for (r = t.left; r.right; ) r = r.right;
return r;
}
const l = this._comparator;
for (; e; ) {
const i = l(t.key, e.key);
if (i === 0) break;
i < 0 ? e = e.left : (r = e, e = e.right);
}
return r;
}
clear() {
return this._root = null, this._size = 0, this;
}
toList() {
return k(this._root);
}
/**
* Bulk-load items. Both array have to be same size
*/
load(t, e = [], r = !1) {
let l = t.length;
const i = this._comparator;
if (r && g(t, e, 0, l - 1, i), this._root === null)
this._root = a(t, e, 0, l), this._size = l;
else {
const o = y(
this.toList(),
x(t, e),
i
);
l = this._size + l, this._root = p({ head: o }, 0, l);
}
return this;
}
isEmpty() {
return this._root === null;
}
get size() {
return this._size;
}
get root() {
return this._root;
}
toString(t = (e) => String(e.key)) {
const e = [];
return _(this._root, "", !0, (r) => e.push(r), t), e.join("");
}
update(t, e, r) {
const l = this._comparator;
let { left: i, right: o } = m(t, this._root, l);
l(t, e) < 0 ? o = c(e, r, o, l) : i = c(e, r, i, l), this._root = w(i, o, l);
}
split(t) {
return m(t, this._root, this._comparator);
}
*[Symbol.iterator]() {
let t = this._root;
const e = [];
let r = !1;
for (; !r; )
t !== null ? (e.push(t), t = t.left) : e.length !== 0 ? (t = e.pop(), yield t, t = t.right) : r = !0;
}
}
function a(n, t, e, r) {
const l = r - e;
if (l > 0) {
const i = e + Math.floor(l / 2), o = n[i], s = t[i], h = new f(o, s);
return h.left = a(n, t, e, i), h.right = a(n, t, i + 1, r), h;
}
return null;
}
function x(n, t) {
const e = new f(null, null);
let r = e;
for (let l = 0; l < n.length; l++)
r = r.next = new f(n[l], t[l]);
return r.next = null, e.next;
}
function k(n) {
let t = n;
const e = [];
let r = !1;
const l = new f(null, null);
let i = l;
for (; !r; )
t ? (e.push(t), t = t.left) : e.length > 0 ? (t = i = i.next = e.pop(), t = t.right) : r = !0;
return i.next = null, l.next;
}
function p(n, t, e) {
const r = e - t;
if (r > 0) {
const l = t + Math.floor(r / 2), i = p(n, t, l), o = n.head;
return o.left = i, n.head = n.head.next, o.right = p(n, l + 1, e), o;
}
return null;
}
function y(n, t, e) {
const r = new f(null, null);
let l = r, i = n, o = t;
for (; i !== null && o !== null; )
e(i.key, o.key) < 0 ? (l.next = i, i = i.next) : (l.next = o, o = o.next), l = l.next;
return i !== null ? l.next = i : o !== null && (l.next = o), r.next;
}
function g(n, t, e, r, l) {
if (e >= r) return;
const i = n[e + r >> 1];
let o = e - 1, s = r + 1;
for (; ; ) {
do
o++;
while (l(n[o], i) < 0);
do
s--;
while (l(n[s], i) > 0);
if (o >= s) break;
let h = n[o];
n[o] = n[s], n[s] = h, h = t[o], t[o] = t[s], t[s] = h;
}
g(n, t, e, s, l), g(n, t, s + 1, r, l);
}
export {
z as default
};
//# sourceMappingURL=splaytree.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
(function(h,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(h=typeof globalThis<"u"?globalThis:h||self,h.SplayTree=c())})(this,(function(){"use strict";class h{constructor(t,e){this.next=null,this.key=t,this.data=e,this.left=null,this.right=null}}function c(n,t){return n>t?1:n<t?-1:0}function u(n,t,e){const r=new h(null,null);let i=r,l=r;for(;;){const o=e(n,t.key);if(o<0){if(t.left===null)break;if(e(n,t.left.key)<0){const s=t.left;if(t.left=s.right,s.right=t,t=s,t.left===null)break}l.left=t,l=t,t=t.left}else if(o>0){if(t.right===null)break;if(e(n,t.right.key)>0){const s=t.right;if(t.right=s.left,s.left=t,t=s,t.right===null)break}i.right=t,i=t,t=t.right}else break}return i.right=t.left,l.left=t.right,t.left=r.right,t.right=r.left,t}function _(n,t,e,r){const i=new h(n,t);if(e===null)return i.left=i.right=null,i;e=u(n,e,r);const l=r(n,e.key);return l<0?(i.left=e.left,i.right=e,e.left=null):l>=0&&(i.right=e.right,i.left=e,e.right=null),i}function d(n,t,e){let r=null,i=null;if(t){t=u(n,t,e);const l=e(t.key,n);l===0?(r=t.left,i=t.right):l<0?(i=t.right,t.right=null,r=t):(r=t.left,t.left=null,i=t)}return{left:r,right:i}}function w(n,t,e){return t===null?n:(n===null||(t=u(n.key,t,e),t.left=n),t)}function a(n,t,e,r,i){if(n){r(`${t}${e?"└── ":"├── "}${i(n)}
`);const l=t+(e?" ":"│ ");n.left&&a(n.left,l,!1,r,i),n.right&&a(n.right,l,!0,r,i)}}class x{constructor(t=c){this._root=null,this._size=0,this._comparator=t}insert(t,e){return this._size++,this._root=_(t,e,this._root,this._comparator)}add(t,e){const r=new h(t,e);this._root===null&&(r.left=r.right=null,this._size++,this._root=r);const i=this._comparator,l=u(t,this._root,i),o=i(t,l.key);return o===0?this._root=l:(o<0?(r.left=l.left,r.right=l,l.left=null):o>0&&(r.right=l.right,r.left=l,l.right=null),this._size++,this._root=r),this._root}remove(t){this._root=this._remove(t,this._root,this._comparator)}_remove(t,e,r){let i;return e===null?null:(e=u(t,e,r),r(t,e.key)===0?(e.left===null?i=e.right:(i=u(t,e.left,r),i.right=e.right),this._size--,i):e)}pop(){let t=this._root;if(t){for(;t.left;)t=t.left;return this._root=u(t.key,this._root,this._comparator),this._root=this._remove(t.key,this._root,this._comparator),{key:t.key,data:t.data}}return null}findStatic(t){let e=this._root;const r=this._comparator;for(;e;){const i=r(t,e.key);if(i===0)return e;i<0?e=e.left:e=e.right}return null}find(t){return this._root&&(this._root=u(t,this._root,this._comparator),this._comparator(t,this._root.key)!==0)?null:this._root}contains(t){let e=this._root;const r=this._comparator;for(;e;){const i=r(t,e.key);if(i===0)return!0;i<0?e=e.left:e=e.right}return!1}forEach(t,e){let r=this._root;const i=[];let l=!1;for(;!l;)r!==null?(i.push(r),r=r.left):i.length!==0?(r=i.pop(),t.call(e,r),r=r.right):l=!0;return this}range(t,e,r,i){const l=[],o=this._comparator;let s=this._root,f;for(;l.length!==0||s;)if(s)l.push(s),s=s.left;else{if(s=l.pop(),f=o(s.key,e),f>0)break;if(o(s.key,t)>=0&&r.call(i,s))return this;s=s.right}return this}keys(){const t=[];return this.forEach(({key:e})=>{t.push(e)}),t}values(){const t=[];return this.forEach(({data:e})=>{t.push(e)}),t}min(){return this._root?this.minNode(this._root).key:null}max(){return this._root?this.maxNode(this._root).key:null}minNode(t=this._root){if(t)for(;t.left;)t=t.left;return t}maxNode(t=this._root){if(t)for(;t.right;)t=t.right;return t}at(t){let e=this._root,r=!1,i=0;const l=[];for(;!r;)if(e)l.push(e),e=e.left;else if(l.length>0){if(e=l.pop(),i===t)return e;i++,e=e.right}else r=!0;return null}next(t){let e=this._root,r=null;if(t.right){for(r=t.right;r.left;)r=r.left;return r}const i=this._comparator;for(;e;){const l=i(t.key,e.key);if(l===0)break;l<0?(r=e,e=e.left):e=e.right}return r}prev(t){let e=this._root,r=null;if(t.left!==null){for(r=t.left;r.right;)r=r.right;return r}const i=this._comparator;for(;e;){const l=i(t.key,e.key);if(l===0)break;l<0?e=e.left:(r=e,e=e.right)}return r}clear(){return this._root=null,this._size=0,this}toList(){return y(this._root)}load(t,e=[],r=!1){let i=t.length;const l=this._comparator;if(r&&m(t,e,0,i-1,l),this._root===null)this._root=p(t,e,0,i),this._size=i;else{const o=z(this.toList(),k(t,e),l);i=this._size+i,this._root=g({head:o},0,i)}return this}isEmpty(){return this._root===null}get size(){return this._size}get root(){return this._root}toString(t=e=>String(e.key)){const e=[];return a(this._root,"",!0,r=>e.push(r),t),e.join("")}update(t,e,r){const i=this._comparator;let{left:l,right:o}=d(t,this._root,i);i(t,e)<0?o=_(e,r,o,i):l=_(e,r,l,i),this._root=w(l,o,i)}split(t){return d(t,this._root,this._comparator)}*[Symbol.iterator](){let t=this._root;const e=[];let r=!1;for(;!r;)t!==null?(e.push(t),t=t.left):e.length!==0?(t=e.pop(),yield t,t=t.right):r=!0}}function p(n,t,e,r){const i=r-e;if(i>0){const l=e+Math.floor(i/2),o=n[l],s=t[l],f=new h(o,s);return f.left=p(n,t,e,l),f.right=p(n,t,l+1,r),f}return null}function k(n,t){const e=new h(null,null);let r=e;for(let i=0;i<n.length;i++)r=r.next=new h(n[i],t[i]);return r.next=null,e.next}function y(n){let t=n;const e=[];let r=!1;const i=new h(null,null);let l=i;for(;!r;)t?(e.push(t),t=t.left):e.length>0?(t=l=l.next=e.pop(),t=t.right):r=!0;return l.next=null,i.next}function g(n,t,e){const r=e-t;if(r>0){const i=t+Math.floor(r/2),l=g(n,t,i),o=n.head;return o.left=l,n.head=n.head.next,o.right=g(n,i+1,e),o}return null}function z(n,t,e){const r=new h(null,null);let i=r,l=n,o=t;for(;l!==null&&o!==null;)e(l.key,o.key)<0?(i.next=l,l=l.next):(i.next=o,o=o.next),i=i.next;return l!==null?i.next=l:o!==null&&(i.next=o),r.next}function m(n,t,e,r,i){if(e>=r)return;const l=n[e+r>>1];let o=e-1,s=r+1;for(;;){do o++;while(i(n[o],l)<0);do s--;while(i(n[s],l)>0);if(o>=s)break;let f=n[o];n[o]=n[s],n[s]=f,f=t[o],t[o]=t[s],t[s]=f}m(n,t,e,s,i),m(n,t,s+1,r,i)}return x}));
//# sourceMappingURL=splaytree.umd.cjs.map

File diff suppressed because one or more lines are too long

2
frontend/node_modules/splaytree/dist/types.d.ts generated vendored Normal file
View File

@@ -0,0 +1,2 @@
export type Key = number | any;
export type Value = any;

82
frontend/node_modules/splaytree/dist/types/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,82 @@
import Node from "./node";
import { Key } from "./types";
export type Comparator<Key> = (a: Key, b: Key) => number;
export type Visitor<Key, Value> = (node: Node<Key, Value>) => undefined | boolean | void;
export type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;
declare function DEFAULT_COMPARE(a: Key, b: Key): number;
export default class Tree<Key = number, Value = any> {
private _comparator;
private _root;
private _size;
constructor(comparator?: typeof DEFAULT_COMPARE);
/**
* Inserts a key, allows duplicates
*/
insert(key: Key, data?: Value): Node<Key, Value>;
/**
* Adds a key, if it is not present in the tree
*/
add(key: Key, data?: Value): Node<Key, Value>;
/**
* @param {Key} key
* @return {Node|null}
*/
remove(key: Key): void;
/**
* Deletes i from the tree if it's there
*/
private _remove;
/**
* Removes and returns the node with smallest key
*/
pop(): {
key: Key;
data: Value;
} | null;
/**
* Find without splaying
*/
findStatic(key: Key): Node<Key, Value> | null;
find(key: Key): Node<Key, Value> | null;
contains(key: Key): boolean;
forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
*/
range(low: Key, high: Key, fn: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
/**
* Returns array of keys
*/
keys(): Key[];
/**
* Returns array of all the data in the nodes
*/
values(): Value[];
min(): Key | null;
max(): Key | null;
minNode(t?: Node<Key, Value> | null): Node<Key, Value> | null;
maxNode(t?: Node<Key, Value> | null): Node<Key, Value> | null;
/**
* Returns node at given index
*/
at(index: number): Node<Key, Value> | null;
next(d: Node<Key, Value>): Node<Key, Value> | null;
prev(d: Node<Key, Value>): Node<Key, Value> | null;
clear(): Tree<Key, Value>;
toList(): Node<any, any>;
/**
* Bulk-load items. Both array have to be same size
*/
load(keys: Key[], values?: Value[], presort?: boolean): this;
isEmpty(): boolean;
get size(): number;
get root(): Node<Key, Value> | null;
toString(printNode?: NodePrinter<Key, Value>): string;
update(key: Key, newKey: Key, newData?: Value): void;
split(key: Key): {
left: Node<import("./types").Key, import("./types").Value> | null;
right: Node<import("./types").Key, import("./types").Value> | null;
};
[Symbol.iterator](): Generator<Node<Key, Value>, void, unknown>;
}
export {};

8
frontend/node_modules/splaytree/dist/types/node.d.ts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
export default class Node<Key, Value> {
key: Key;
data: any;
left: Node<Key, Value> | null;
right: Node<Key, Value> | null;
next: Node<Key, Value> | null;
constructor(key: Key, data?: any);
}

View File

@@ -0,0 +1,2 @@
export type Key = number | any;
export type Value = any;

62
frontend/node_modules/splaytree/package.json generated vendored Normal file
View File

@@ -0,0 +1,62 @@
{
"name": "splaytree",
"version": "3.2.3",
"author": "Alexander Milevski <info@w8r.name>",
"license": "MIT",
"description": "Fast Splay tree for Node and browser",
"type": "module",
"main": "dist/splaytree.umd.cjs",
"browser": "dist/splaytree.umd.cjs",
"unpkg": "dist/splaytree.umd.cjs",
"module": "dist/splaytree.js",
"types": "dist/types/index.d.ts",
"files": [
"dist",
"src"
],
"directories": {
"test": "tests"
},
"repository": {
"type": "git",
"url": "https://github.com/w8r/splay-tree.git"
},
"scripts": {
"build": "vite build && npm run types",
"types": "tsc --declaration --emitDeclarationOnly -p tsconfig.build.json",
"test": "vitest",
"test:watch": "vitest watch",
"bench": "vitest bench",
"coverage": "vitest run --coverage",
"lint": "oxlint src tests",
"prepublishOnly": "npm run build && npm test",
"clean": "rm -rf dist coverage .nyc"
},
"devDependencies": {
"@vitest/coverage-v8": "4.0.16",
"avl": "2.0.0",
"bintrees": "1.0.2",
"oxlint": "1.35.0",
"typescript": "5.8.2",
"vite": "7.3.0",
"vite-plugin-dts": "4.5.4",
"vitest": "4.0.16"
},
"keywords": [
"binary-tree",
"bst",
"splay-tree",
"splay",
"balanced-search-tree"
],
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/splaytree.js",
"require": "./dist/splaytree.umd.cjs"
}
},
"engines": {
"node": ">=18.20 || >=20"
}
}

684
frontend/node_modules/splaytree/src/index.ts generated vendored Normal file
View File

@@ -0,0 +1,684 @@
import Node from "./node";
import { Key, Value } from "./types";
export type Comparator<Key> = (a: Key, b: Key) => number;
export type Visitor<Key, Value> = (
node: Node<Key, Value>
) => undefined | boolean | void;
export type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;
/* follows "An implementation of top-down splaying"
* by D. Sleator <sleator@cs.cmu.edu> March 1992
*/
function DEFAULT_COMPARE(a: Key, b: Key): number {
return a > b ? 1 : a < b ? -1 : 0;
}
type TreeNodeList<Key, Value> = { head: Node<Key, Value> | null };
/**
* Simple top down splay, not requiring i to be in the tree t.
*/
function splay(
i: Key,
t: Node<Key, Value>,
comparator: Comparator<Key>
): Node<Key, Value> {
const N = new Node(null, null);
let l = N;
let r = N;
while (true) {
const cmp = comparator(i, t!.key);
//if (i < t.key) {
if (cmp < 0) {
if (t!.left === null) break;
//if (i < t.left.key) {
if (comparator(i, t.left.key) < 0) {
const y = t.left; /* rotate right */
t.left = y.right;
y.right = t;
t = y;
if (t.left === null) break;
}
r.left = t; /* link right */
r = t;
t = t.left;
//} else if (i > t.key) {
} else if (cmp > 0) {
if (t.right === null) break;
//if (i > t.right.key) {
if (comparator(i, t.right.key) > 0) {
const y = t.right; /* rotate left */
t.right = y.left;
y.left = t;
t = y;
if (t.right === null) break;
}
l.right = t; /* link left */
l = t;
t = t.right;
} else break;
}
/* assemble */
l.right = t.left;
r.left = t.right;
t.left = N.right;
t.right = N.left;
return t;
}
function insert(
i: Key,
data: Value,
t: Node<Key, Value>,
comparator: Comparator<Key>
): Node<Key, Value> {
const node = new Node(i, data);
if (t === null) {
node.left = node.right = null;
return node;
}
t = splay(i, t, comparator);
const cmp = comparator(i, t.key);
if (cmp < 0) {
node.left = t.left;
node.right = t;
t.left = null;
} else if (cmp >= 0) {
node.right = t.right;
node.left = t;
t.right = null;
}
return node;
}
function split(
key: Key,
v: Node<Key, Value>,
comparator: Comparator<Key>
): {
left: Node<Key, Value> | null;
right: Node<Key, Value> | null;
} {
let left = null;
let right = null;
if (v) {
v = splay(key, v, comparator);
const cmp = comparator(v.key, key);
if (cmp === 0) {
left = v.left;
right = v.right;
} else if (cmp < 0) {
right = v.right;
v.right = null;
left = v;
} else {
left = v.left;
v.left = null;
right = v;
}
}
return { left, right };
}
function merge(
left: Node<Key, Value> | null,
right: Node<Key, Value> | null,
comparator: Comparator<Key>
) {
if (right === null) return left;
if (left === null) return right;
right = splay(left.key, right, comparator);
right.left = left;
return right;
}
type StringCollector = (s: string) => void;
/**
* Prints level of the tree
*/
function printRow(
root: Node<Key, Value>,
prefix: string,
isTail: boolean,
out: StringCollector,
printNode: NodePrinter<Key, Value>
) {
if (root) {
out(`${prefix}${isTail ? "└── " : "├── "}${printNode(root)}\n`);
const indent = prefix + (isTail ? " " : "│ ");
if (root.left) printRow(root.left, indent, false, out, printNode);
if (root.right) printRow(root.right, indent, true, out, printNode);
}
}
export default class Tree<Key = number, Value = any> {
private _comparator: Comparator<Key>;
private _root: Node<Key, Value> | null = null;
private _size: number = 0;
constructor(comparator = DEFAULT_COMPARE) {
this._comparator = comparator;
}
/**
* Inserts a key, allows duplicates
*/
public insert(key: Key, data?: Value): Node<Key, Value> {
this._size++;
return (this._root = insert(key, data, this._root!, this._comparator));
}
/**
* Adds a key, if it is not present in the tree
*/
public add(key: Key, data?: Value): Node<Key, Value> {
const node = new Node(key, data);
if (this._root === null) {
node.left = node.right = null;
this._size++;
this._root = node;
}
const comparator = this._comparator;
const t = splay(key, this._root, comparator);
const cmp = comparator(key, t.key);
if (cmp === 0) this._root = t;
else {
if (cmp < 0) {
node.left = t.left;
node.right = t;
t.left = null;
} else if (cmp > 0) {
node.right = t.right;
node.left = t;
t.right = null;
}
this._size++;
this._root = node;
}
return this._root;
}
/**
* @param {Key} key
* @return {Node|null}
*/
public remove(key: Key): void {
this._root = this._remove(key, this._root!, this._comparator);
}
/**
* Deletes i from the tree if it's there
*/
private _remove(i: Key, t: Node<Key, Value>, comparator: Comparator<Key>) {
let x;
if (t === null) return null;
t = splay(i, t, comparator);
const cmp = comparator(i, t.key);
if (cmp === 0) {
/* found it */
if (t.left === null) {
x = t.right!;
} else {
x = splay(i, t.left, comparator);
x.right = t.right;
}
this._size--;
return x;
}
return t; /* It wasn't there */
}
/**
* Removes and returns the node with smallest key
*/
public pop(): { key: Key; data: Value } | null {
let node = this._root;
if (node) {
while (node.left) node = node.left;
this._root = splay(node.key, this._root!, this._comparator);
this._root = this._remove(node.key, this._root, this._comparator);
return { key: node.key, data: node.data };
}
return null;
}
/**
* Find without splaying
*/
public findStatic(key: Key): Node<Key, Value> | null {
let current = this._root;
const compare = this._comparator;
while (current) {
const cmp = compare(key, current.key);
if (cmp === 0) return current;
else if (cmp < 0) current = current.left;
else current = current.right;
}
return null;
}
public find(key: Key): Node<Key, Value> | null {
if (this._root) {
this._root = splay(key, this._root, this._comparator);
if (this._comparator(key, this._root.key) !== 0) return null;
}
return this._root;
}
public contains(key: Key): boolean {
let current = this._root;
const compare = this._comparator;
while (current) {
const cmp = compare(key, current.key);
if (cmp === 0) return true;
else if (cmp < 0) current = current.left;
else current = current.right;
}
return false;
}
public forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value> {
let current = this._root;
const Q = []; /* Initialize stack s */
let done = false;
while (!done) {
if (current !== null) {
Q.push(current);
current = current.left;
} else {
if (Q.length !== 0) {
current = Q.pop()!;
visitor.call(ctx, current);
current = current.right;
} else done = true;
}
}
return this;
}
/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
*/
public range(
low: Key,
high: Key,
fn: Visitor<Key, Value>,
ctx?: any
): Tree<Key, Value> {
const Q = [];
const compare = this._comparator;
let node = this._root;
let cmp;
while (Q.length !== 0 || node) {
if (node) {
Q.push(node);
node = node.left;
} else {
node = Q.pop()!;
cmp = compare(node.key, high);
if (cmp > 0) {
break;
} else if (compare(node.key, low) >= 0) {
if (fn.call(ctx, node)) return this; // stop if smth is returned
}
node = node.right;
}
}
return this;
}
/**
* Returns array of keys
*/
public keys(): Key[] {
const keys: Key[] = [];
this.forEach(({ key }) => {
keys.push(key);
});
return keys;
}
/**
* Returns array of all the data in the nodes
*/
public values(): Value[] {
const values: Value[] = [];
this.forEach(({ data }) => {
values.push(data);
});
return values;
}
public min(): Key | null {
if (this._root) return this.minNode(this._root)!.key;
return null;
}
public max(): Key | null {
if (this._root) return this.maxNode(this._root)!.key;
return null;
}
public minNode(t = this._root): Node<Key, Value> | null {
if (t) while (t.left) t = t.left;
return t;
}
public maxNode(t = this._root): Node<Key, Value> | null {
if (t) while (t.right) t = t.right;
return t;
}
/**
* Returns node at given index
*/
public at(index: number): Node<Key, Value> | null {
let current = this._root;
let done = false;
let i = 0;
const Q = [];
while (!done) {
if (current) {
Q.push(current);
current = current.left;
} else {
if (Q.length > 0) {
current = Q.pop()!;
if (i === index) return current;
i++;
current = current.right!;
} else done = true;
}
}
return null;
}
public next(d: Node<Key, Value>): Node<Key, Value> | null {
let root = this._root;
let successor = null;
if (d.right) {
successor = d.right;
while (successor.left) successor = successor.left;
return successor;
}
const comparator = this._comparator;
while (root) {
const cmp = comparator(d.key, root.key);
if (cmp === 0) break;
else if (cmp < 0) {
successor = root;
root = root.left;
} else root = root.right;
}
return successor;
}
public prev(d: Node<Key, Value>): Node<Key, Value> | null {
let root = this._root;
let predecessor = null;
if (d.left !== null) {
predecessor = d.left;
while (predecessor.right) predecessor = predecessor.right;
return predecessor;
}
const comparator = this._comparator;
while (root) {
const cmp = comparator(d.key, root.key);
if (cmp === 0) break;
else if (cmp < 0) root = root.left;
else {
predecessor = root;
root = root.right;
}
}
return predecessor;
}
public clear(): Tree<Key, Value> {
this._root = null;
this._size = 0;
return this;
}
public toList() {
return toList(this._root!);
}
/**
* Bulk-load items. Both array have to be same size
*/
public load(keys: Key[], values: Value[] = [], presort: boolean = false) {
let size = keys.length;
const comparator = this._comparator;
// sort if needed
if (presort) sort(keys, values, 0, size - 1, comparator);
if (this._root === null) {
// empty tree
this._root = loadRecursive(keys, values, 0, size);
this._size = size;
} else {
// that re-builds the whole tree from two in-order traversals
const mergedList = mergeLists(
this.toList(),
createList(keys, values),
comparator
);
size = this._size + size;
this._root = sortedListToBST({ head: mergedList }, 0, size);
}
return this;
}
public isEmpty(): boolean {
return this._root === null;
}
get size(): number {
return this._size;
}
get root(): Node<Key, Value> | null {
return this._root;
}
public toString(
printNode: NodePrinter<Key, Value> = (n) => String(n.key)
): string {
const out: string[] = [];
printRow(this._root!, "", true, (v) => out.push(v), printNode);
return out.join("");
}
public update(key: Key, newKey: Key, newData?: Value): void {
const comparator = this._comparator;
let { left, right } = split(key, this._root!, comparator);
if (comparator(key, newKey) < 0) {
right = insert(newKey, newData, right!, comparator);
} else {
left = insert(newKey, newData, left!, comparator);
}
this._root = merge(left, right, comparator);
}
public split(key: Key) {
return split(key, this._root!, this._comparator);
}
*[Symbol.iterator]() {
let current = this._root;
const Q: Node<Key, Value>[] = []; /* Initialize stack s */
let done = false;
while (!done) {
if (current !== null) {
Q.push(current);
current = current.left;
} else {
if (Q.length !== 0) {
current = Q.pop()!;
yield current;
current = current.right;
} else done = true;
}
}
}
}
function loadRecursive(
keys: Key[],
values: Value[],
start: number,
end: number
): Node<Key, Value> | null {
const size = end - start;
if (size > 0) {
const middle = start + Math.floor(size / 2);
const key = keys[middle];
const data = values[middle];
const node = new Node(key, data);
node.left = loadRecursive(keys, values, start, middle);
node.right = loadRecursive(keys, values, middle + 1, end);
return node;
}
return null;
}
function createList(keys: Key[], values: Value[]): Node<Key, Value> {
const head = new Node<Key, Value>(null, null);
let p: Node<Key, Value> = head;
for (let i = 0; i < keys.length; i++) {
p = p.next = new Node(keys[i], values[i]);
}
p.next = null;
return head.next!;
}
function toList(root: Node<Key, Value>): Node<Key, Value> {
let current = root;
const Q = [];
let done = false;
const head = new Node<Key, Value>(null, null);
let p = head;
while (!done) {
if (current) {
Q.push(current);
current = current.left!;
} else {
if (Q.length > 0) {
current = p = p.next = Q.pop()!;
current = current.right!;
} else done = true;
}
}
p.next = null; // that'll work even if the tree was empty
return head.next!;
}
function sortedListToBST(
list: TreeNodeList<Key, Value>,
start: number,
end: number
) {
const size = end - start;
if (size > 0) {
const middle = start + Math.floor(size / 2);
const left = sortedListToBST(list, start, middle);
const root = list.head;
root!.left = left;
list.head = list.head!.next;
root!.right = sortedListToBST(list, middle + 1, end);
return root!;
}
return null;
}
function mergeLists<Key, Value>(
l1: Node<Key, Value>,
l2: Node<Key, Value>,
compare: Comparator<Key>
): Node<Key, Value> {
const head: Node<Key, Value> = new Node<Key, Value>(null as Key, null); // dummy
let p = head;
let p1: Node<Key, Value> = l1;
let p2: Node<Key, Value> = l2;
while (p1 !== null && p2 !== null) {
if (compare(p1.key, p2.key) < 0) {
p.next = p1;
p1 = p1.next!;
} else {
p.next = p2;
p2 = p2.next!;
}
p = p.next;
}
if (p1 !== null) {
p.next = p1;
} else if (p2 !== null) {
p.next = p2;
}
return head.next!;
}
function sort(
keys: Key[],
values: Value[],
left: number,
right: number,
compare: Comparator<Key>
) {
if (left >= right) return;
const pivot = keys[(left + right) >> 1];
let i = left - 1;
let j = right + 1;
while (true) {
do i++;
while (compare(keys[i], pivot) < 0);
do j--;
while (compare(keys[j], pivot) > 0);
if (i >= j) break;
let tmp = keys[i];
keys[i] = keys[j];
keys[j] = tmp;
tmp = values[i];
values[i] = values[j];
values[j] = tmp;
}
sort(keys, values, left, j, compare);
sort(keys, values, j + 1, right, compare);
}

15
frontend/node_modules/splaytree/src/node.ts generated vendored Normal file
View File

@@ -0,0 +1,15 @@
export default class Node<Key, Value> {
public key:Key;
public data:any;
public left:Node<Key, Value>|null;
public right:Node<Key, Value>|null;
public next:Node<Key, Value>|null = null;
constructor (key:Key, data?:any) {
this.key = key;
this.data = data;
this.left = null;
this.right = null;
}
}

2
frontend/node_modules/splaytree/src/types.ts generated vendored Normal file
View File

@@ -0,0 +1,2 @@
export type Key = number|any;
export type Value = any;