Skip to content

Instantly share code, notes, and snippets.

View yongjun21's full-sized avatar

Yong Jun yongjun21

  • Singapore
  • 04:44 (UTC +08:00)
View GitHub Profile
@yongjun21
yongjun21 / MultiClassBitmask.ts
Created October 1, 2024 08:16
Data structure for handling multiple bitmasks within one stack
import _noop from "lodash/noop";
import SparseBitmask from "./SparseBitmask";
import { OrderedSet, OrderedMap } from "./OrderedCollections";
interface RunNode {
currState: OrderedSet<number>;
diff: Set<number>;
pending: Set<number>;
newFlag: boolean;
@yongjun21
yongjun21 / bitmaskHelpers.ts
Last active October 1, 2024 08:00
Some bitmask helpers
import SparseBitmask, {
unionRunEnds,
intersectionRunEnds,
subtractionRunEnds,
} from "./SparseBitmask";
import Bitmask from "./Bitmask";
import { memoized, numberHasher } from "./memo";
import { OrderedSet } from "./OrderedCollections";
interface TransformedPolygon {
@yongjun21
yongjun21 / SparseBitmask.ts
Last active October 1, 2024 07:58
Custom data structure for efficient manipulation of sparse bitmask
import {
iterFilter,
iterFilterIndex,
ArrayConstructor,
ArrayLike,
} from "./base";
import { OrderedSet } from "./OrderedCollections";
export default class SparseBitmask {
@yongjun21
yongjun21 / OrderedCollections.ts
Last active October 1, 2024 07:55
Combining heap with hash table to get a data structure that supports fast iteration even with frequent insert and delete
export class OrderedSet<T> {
private heap: T[] = [];
private sorted: T[] = [];
private heapIndex = new Map<T, number>();
private comparator: (item: T) => any;
constructor(comparator: (item: T) => any = item => item) {
this.comparator = comparator;
}
@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;
@yongjun21
yongjun21 / encode_octree.ipynb
Last active July 25, 2022 15:23
Fast (vectorized) implementation of Octree encoding
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yongjun21
yongjun21 / minheap.js
Last active December 18, 2021 08:27
MinHeap in JS
class MinHeap {
constructor (comparator = (a, b) => a - b) {
this._data = []
this._comparator = comparator
}
push (...values) {
for (const v of values) {
let cIndex = this._data.push(v) - 1
while (cIndex > 0) {
@yongjun21
yongjun21 / euclidean.ipynb
Last active September 24, 2020 11:52
Neat little trick to efficiently compute Euclidean distance in pandas
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yongjun21
yongjun21 / Promise.filter.js
Last active September 4, 2024 06:28
Complete implementation of Promise.filter
Promise.filter = function (iterable, filterer, options = {}) {
let concurrency = options.concurrency || Infinity
let index = 0
const results = []
const predicates = []
const pending = []
const iterator = iterable[Symbol.iterator]()
while (concurrency-- > 0) {