Skip to content

Instantly share code, notes, and snippets.

View yongjun21's full-sized avatar

Yong Jun yongjun21

  • Singapore
  • 15:02 (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 July 22, 2026 09:56
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 August 24, 2026 10:27
Custom data structure for efficient manipulation of sparse bitmask
/**
* Companion modules:
* - 2D operations and analysis:
* https://gist.github.com/yongjun21/567dc9d60b3994587f0109e85537f24c
* - 3D transforms and spatial operations:
* https://gist.github.com/yongjun21/5162276446d90416192e6365c7a6c2e4
* - Multiclass layer compositing:
* https://gist.github.com/yongjun21/9c9f6b9c2ec1e38f5c80d29ead3af864
*
* Together, these modules form an end-to-end sparse-mask processing system.
@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 August 24, 2026 11:16
Fast Octree decoder in JS
/**
* Companion Python encoder:
* https://gist.github.com/yongjun21/17ebb9d041b0784e76c99e5cae0b6ac0
*
* Together, the encoder and decoder form the point-cloud Octree codec
* used for compact storage, streaming and browser-based review.
*/
const MAX_LEVEL = 30;
const STACK = new Float32Array((7 * MAX_LEVEL + 1) * 4);