This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Tree<T, K> = T & { | |
id: K | |
children: Tree<T, K>[] | |
} | |
// Based on KMP algorithm | |
export function matchTree<T, K>(root: Tree<T, K>, selector: K[]) { | |
const lps = createLPS(selector) | |
const st = [[root, 0]] as [Tree<T, K>, number][] | |
const results = [] as Tree<T, K>[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Remove<R extends unknown[], I extends number, Out extends unknown[] = [], Count extends unknown[] = []> | |
= R extends [] | |
? Out | |
: R extends [infer First, ...infer Rest] | |
? Count['length'] extends I | |
? Remove<Rest, I, Out, [...Count, 0]> | |
: Remove<Rest, I, [...Out, First], [...Count, 0]> | |
: never | |
type X = Remove<['a', 'b', 'c'], 1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RecursiveArray<T> = (T | RecursiveArray<T>)[] | |
type UtilizedRecursiveArray<T> = RecursiveArray<T> & { | |
get(...indices: number[]): T | RecursiveArray<T> | |
set(value: T, ...indices: number[]): T | |
} | |
function utilize<T>(rarr: RecursiveArray<T>): UtilizedRecursiveArray<T> { | |
const res = rarr as any | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BinaryTrieNode<T> { | |
value?: T | |
lc?: BinaryTrieNode<T> | |
rc?: BinaryTrieNode<T> | |
} | |
class BinaryTrie<T> { | |
root: BinaryTrieNode<T> = {} | |
constructor(public readonly depth: number) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function* combinateWithoutReplacement<T>(candidates: T[]) { | |
const indices = candidates.map((_, i) => i) | |
yield indices.map((i) => candidates[i]) | |
const c = indices.map(() => 0) | |
let i = 1 | |
while (i < indices.length) { | |
if (c[i] < i) { | |
if (i % 2 === 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function* combinateWithReplacement<T>(candidates: T[], length: number) { | |
if (length <= 0) return | |
for (let i = 0; i < candidates.length ** length; ++i) { | |
const result = new Array<T>(length) | |
let j = i | |
for (let k = length - 1; k >= 0; --k) { | |
result[k] = candidates[j % candidates.length] | |
j = Math.floor(j / candidates.length) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AnchorHTMLAttributes, MouseEvent as ReactMouseEvent, useCallback } from 'react' | |
const InternalUrlRegexp = /^\.?\// | |
const StartingDotRegexp = /^\./ | |
export function Anchor({ | |
children, | |
href, | |
onClick, | |
...rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* permutate<T>(domain: T[], length: number) { | |
function* recurse(stack: T[] = [], depth: number = 0): Generator<T[]> { | |
if (depth >= length) { | |
yield stack.slice() | |
return | |
} | |
for (const element of domain) { | |
stack[depth] = element | |
yield* recurse(stack, depth + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CumulativeSum { | |
private sums: number[] = [0]; | |
constructor(itr: Iterable<number>) { | |
const values = [...itr]; | |
for (let i = 0; i < values.length; ++i) { | |
this.sums[i + 1] = this.sums[i] + values[i]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Vector2 } from 'three'; | |
export function findCircumCircle(vertices: Vector2[]) { | |
if (vertices.length === 1) { | |
return [vertices[0], 0]; | |
} | |
if (vertices.length === 2) { | |
return [ | |
vertices[0].clone().add(vertices[1]).multiplyScalar(0.5), | |
vertices[0].distanceTo(vertices[1]) * 0.5, |
NewerOlder