Skip to content

Instantly share code, notes, and snippets.

View yongjun21's full-sized avatar

Yong Jun yongjun21

  • Singapore
  • 15:27 (UTC +08:00)
View GitHub Profile
@yongjun21
yongjun21 / async.ts
Created February 6, 2026 04:34
Utilities for handling async operations
import { LinkedList } from "./base";
import { OrderedSet } from "./OrderedCollections";
export function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function nextTick(): Promise<void> {
return new Promise(resolve => {
queueMicrotask(() => {
@yongjun21
yongjun21 / useWorker.ts
Last active February 6, 2026 04:19
Utility to simplify Web Worker use
type Callable = (...args: any[]) => any | Promise<any>;
type Streamable = (...args: any[]) => AsyncIterableIterator<any>;
interface Handler {
[key: string]: any;
}
interface Callbacks {
resolve: (value: any) => void;
reject: (reason?: any) => void;
@yongjun21
yongjun21 / bitmaskHelpers3d.ts
Last active April 15, 2026 04:13
A set of helper functions for manipulating 3D bitmask in sparse (run-based) format
import SparseBitmask from "./SparseBitmask";
import { bitmaskFromWeightedRuns } from "./bitmaskHelpers";
import { OrderedMap } from "./OrderedCollections";
import { range } from "./base";
/**
* [Claude generated]
*
* `cutAndPasteBitmask3d` copies a 3-D sub-volume (the "cut" box) out of `from`
* and XOR-pastes it into `to` at an offset position. Both the cut and paste
@yongjun21
yongjun21 / perfectFit.ts
Created March 10, 2025 05:38
Calculates an ideal viewBox that centers on a target object
/**
* Calculates an ideal viewBox that centers on a target object
* @param objectBBox - Bounding box of the target object
* @param assetWidth - Width of the asset
* @param assetHeight - Height of the asset
* @param targetCoverage - target percentage of the viewport width or height the object should take up
* @param viewportAspectRatio - Aspect ratio of the viewport
* @param minFit - Minimum width and height of the viewport
* @returns - [offsetX, offsetY, viewportWidth, viewportHeight]
*/
@yongjun21
yongjun21 / MultiClassBitmask.ts
Last active April 15, 2026 04:24
Data structure for handling multiple bitmasks within one stack
/**
* [Claude generated]
*
* MultiClassBitmask is a layered compositing system for SparseBitmasks think
* Photoshop layers for bitmasks. Any number of SparseBitmask layers can be
* stacked with integer priorities; at each pixel the highest-priority layer
* that covers it "wins" and determines the rendered value T via a PixelShader.
* The default shader returns the top mask object itself.
*
* --- Internal representation ---
@yongjun21
yongjun21 / bitmaskHelpers.ts
Last active February 3, 2026 04:59
Some helpers functions for manipulating bitmask in sparse (run-based) format
import SparseBitmask, {
unionRunEnds,
intersectionRunEnds,
subtractionRunEnds,
} from "./SparseBitmask";
import {
cutAndPasteBitmask3d,
getBoundingBox3d,
getCentroid3d,
getWindowedRunLengths3d,
@yongjun21
yongjun21 / SparseBitmask.ts
Last active April 15, 2026 04:13
Custom data structure for efficient manipulation of sparse bitmask
/**
* [Claude generated]
*
* SparseBitmask is a fixed-length bitmask optimised for sparse, run-heavy data.
* It supports the usual bitwise operations
* union, intersection, subtraction, symmetric diff plus
* mutation variants that fire a callback only on the bits that changed, which
* is useful for driving incremental UI updates. Default iteration (`for…of`,
* `forEach`, `map`) yields each bit value (0 or 1) across the full `length`,
* but in practice `getIndices()` and `getIndicesCount()` are used more often
@yongjun21
yongjun21 / OrderedCollections.ts
Last active April 15, 2026 04:13
Combining heap with hash table to get a data structure that supports fast iteration even with frequent insert and delete
/**
* [Claude generated]
*
* This file implements two data structures OrderedSet and OrderedMap that
* combine a min-heap with a hash map so you get O(log n) insert/delete AND
* cheap O(1) membership tests, plus the ability to iterate in sorted order
* without paying a full sort each time.
*
* --- OrderedSet ---
* Internally it maintains two arrays: `heap` (unsorted, heap-ordered) and
@yongjun21
yongjun21 / bitmask.ts
Last active February 6, 2024 02:49
Memory efficient implementation of bitmask
import { Stack } from './common.js';
export function decodeBitmask(
encoded: Uint8Array,
maxIndex: number
): Iterable<number> {
return {
*[Symbol.iterator]() {
const n = maxIndex + 1;
const depth = Math.ceil(Math.log2(n));
@yongjun21
yongjun21 / decodeOctree.ts
Last active July 25, 2022 16:53
Fast Octree decoder in JS
const MAX_LEVEL = 30;
const STACK = new Float32Array((7 * MAX_LEVEL + 1) * 4);
interface OctreeHeader {
version: number;
precision: number;
maxLevel: number;
nodeCounts: number[];
leafCount: number;
dataStartOffset: number;