Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@Phryxia
Phryxia / diff.ts
Last active February 27, 2023 05:30
TypeScript implementation of computing difference of given two JavaScript value
type Addition = {
type: 'addition'
next: any
}
type Change = {
type: 'change'
prev: any
next: any
}
@Phryxia
Phryxia / adjacentPairs.ts
Last active September 30, 2024 19:20
TypeScript implementation of simple adjacent iterator
export function adjacentPairs<T>(
itr: Iterable<T>,
n: number,
isClosed?: boolean,
): IterableIterator<T[]> {
const buffer = new Array<T>(n);
const it = itr[Symbol.iterator]();
let res = it.next();
for (let i = 0; i < n; ++i) {
@Phryxia
Phryxia / undirectedMultiGraph.ts
Created July 6, 2023 17:12
TypeScript implementation of undirected sparse multi graph
export interface Neighbor {
index: number;
count: number;
}
export class UndirectedMultiGraph {
edges = [] as Neighbor[][];
addEdge(u: number, v: number) {
this.connectDirected(u, v);
@Phryxia
Phryxia / eulerPath.ts
Created July 6, 2023 17:12
TypeScript implementation of euler path finder
// Use https://gist.github.com/Phryxia/72f336275a9807e7797d93fc31673393
import { UndirectedMultiGraph } from './UndirectedMultiGraph';
/**
* Let v1, v2, ..., vn be vertices of unknown undirected multi graph.
* This function find any euler path using only given random-ordered edges.
* If there is euler circuit, return circuit.
* If there is no euler path or circuit, the behavior is undefined.
*
* @param edges
@Phryxia
Phryxia / decideCCW.ts
Last active July 15, 2023 14:11
TypeScript implementation of deciding whether 2d circuit is counter-clockwise
export function decideCCW(...path: Vector2[]) {
if (path.length < 3) return 0;
const yBase = path.reduce((acc, v) => Math.min(acc, v.y), Infinity);
// https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order
let sum = 0;
for (const [v1, v2] of adjacentPairs(path, 2, true)) {
const area = (v2.x - v1.x) * (v2.y + v1.y - 2 * yBase);
sum += area;
@Phryxia
Phryxia / getDistandcesForDAG.ts
Created July 23, 2023 06:53
TypeScript implementation of maximum distance finder for directed acyclic graph(DAG)
interface Edge {
neighbor: number
isReversed?: boolean
}
interface Graph {
numberOfVertices: number
edges: Edge[][]
}
@Phryxia
Phryxia / SymmetricMatrix.ts
Last active May 15, 2024 02:55
TypeScript implementation of simple SymmetricMatrix
export class SymmetricMatrix<T> {
protected data: T[];
constructor(
public readonly size: number,
initializer: (i: number, j: number) => T,
) {
this.data = new Array(size);
for (let i = 0; i < size; ++i) {
@Phryxia
Phryxia / flooding.ts
Created August 9, 2023 16:02
TypeScript implementation of universal flooding algorithm
interface FloodingParams<T> {
seeds: T[];
spanner(seed: T): T[];
serializer(seed: T): number | string;
iterationLimit?: number;
}
export function flooding<T>({
seeds,
spanner,
@Phryxia
Phryxia / circumcircle.ts
Created August 13, 2023 11:32
TypeScript implementation of circumcircle finder
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,
@Phryxia
Phryxia / cumulativeSum.ts
Created August 13, 2023 11:54
TypeScript implementation of fast cumulative sum
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];
}
}