Skip to content

Instantly share code, notes, and snippets.

View yongjun21's full-sized avatar

Yong Jun yongjun21

  • Singapore
  • 17:59 (UTC +08:00)
View GitHub Profile
@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
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
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 May 29, 2023 02:58
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) {
@yongjun21
yongjun21 / Promise.map.js
Last active May 29, 2023 02:55
Complete implementation of Promise.map
Promise.map = function (iterable, mapper, options = {}) {
let concurrency = options.concurrency || Infinity
let index = 0
const results = []
const pending = []
const iterator = iterable[Symbol.iterator]()
while (concurrency-- > 0) {
const thread = wrappedMapper()
@yongjun21
yongjun21 / delay.js
Last active May 29, 2023 02:59
Alternative implementation of timed delay decorators
function delay (invoke, ms) {
return (...args) => new Promise(resolve => {
setTimeout(resolve, ms)
}).then(() => invoke.apply(...args))
}
function delay2 (invoke, ms) {
return (...args) => new Promise(resolve => {
setTimeout(resolve, ms, invoke(...args))
})
@yongjun21
yongjun21 / Promise.waterfall.js
Last active May 29, 2023 03:00
Simple Promise.waterfall implementation
Promise.waterfall = function (array, invoke) {
let pending = Promise.resolve()
const results = []
for (const item of array) {
pending = pending
.then(() => invoke(item, i))
.then(result => results.push(result))
}
@yongjun21
yongjun21 / Tooltip.vue
Created September 25, 2019 04:01
Tooltip that tracks position of a target element using mutation observer
<template>
<div class="tooltip" :style="style">
<slot></slot>
</div>
</template>
<script>
export default {
props: {
target: {