Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@Phryxia
Phryxia / CircularQueue.js
Last active October 1, 2022 02:54
Simple CircularQueue implementation for JavaScript/TypeScript
class CircularQueue {
constructor(growthRate) {
this.growthRate = growthRate
this.data = new Array(4);
this.head = 0;
this.tail = 0;
this.size = 0;
}
grow() {
@Phryxia
Phryxia / Trie.js
Created January 12, 2022 08:41
Simple Trie implementation for JavasScript/TypeScript
class Trie {
constructor() {
this.root = { children: {} };
this.size = 0;
}
add(key, value) {
let node = this.root;
for (let i = 0; i < key.length; ++i) {
if (!node.children[key[i]]) node.children[key[i]] = { children: {} };
@Phryxia
Phryxia / useDialatedPromises.ts
Last active February 7, 2022 17:02
Custom react hook for executing batch promises and retrieve its result in asynchronous way with minimum rendering.
import { useEffect, useReducer, useRef, useState } from 'react'
interface DialationState {
counter: number
called: number
resolved: number
timer?: NodeJS.Timeout
isCalling: boolean
}
@Phryxia
Phryxia / Heap.js
Last active February 3, 2022 03:15
Simple Heap implementation for JavaScript/TypeScript
class Heap {
constructor(comparator) {
this.data = []
this.comparator = comparator
}
push(value) {
this.data.push(value)
this.siftUp(this.size() - 1)
return value
@Phryxia
Phryxia / bfs.ts
Last active February 3, 2022 04:04
Generalized BFS(Breadth-First-Search) implementation for TypeScript
import { CircularQueue } from './somewhere'
export enum SearchEvaluation {
SUCCESS = 'SUCCESS',
KEEP = 'KEEP',
TERMINATE = 'TERMINATE',
}
export interface SearchChoice<S> {
nextState: S
@Phryxia
Phryxia / integerMaps.ts
Last active March 7, 2022 09:45
Provides some interesting map functions between natural and integer set.
// Let N := Z+ ∪ {0}
// N → Z
export function mapNaturalToInteger(x: number): number {
return x % 2 === 0 ? x / 2 : -(x + 1) / 2
}
// Z → N
export function mapIntegerToNatural(x: number): number {
return x >= 0 ? x * 2 : -2 * x - 1
@Phryxia
Phryxia / topologicalSort.ts
Last active March 7, 2022 15:25
DFS based topological sort implementation for TypeScript
type Id = number
export interface Dependency {
id: Id
dependencies: Id[]
}
const PENDING = 1
const WRITTEN = 2
@Phryxia
Phryxia / dfs.ts
Last active April 10, 2022 02:41
Generalized DFS(Depth-First-Search) implementation for TypeScript
type KeyType = number | string | symbol
export interface DFSProcess<T> {
state: T
description?: string
}
export interface DFSNode<T> {
id: KeyType
state: T
@Phryxia
Phryxia / getSamplesWithoutReplacement.ts
Last active January 17, 2024 07:28
Extract samples without replacement using TypeScript.
/**
* Extract n samples from list without replacement in O(|list|)
* @param {Object[]} list - array containing elements
* @param {number} n - size of the extraction
* @returns {Object[]} new array contains extracted elements
*/
function getSamplesWithoutReplacement<T>(list: T[], n: number): T[] {
if (n <= 0) return []
list = [...list]
@Phryxia
Phryxia / findGreatestElement.ts
Last active March 27, 2022 10:03
TypeScript implementation of finding greatest element in partially ordered set.
export type Relation = [string, string]
function makeGraph(relations: Relation[]): Record<string, string[]> {
const result: Record<string, string[]> = {}
relations.forEach(([u, v]) => {
(result[u] ??= []).push(v)
result[v] ??= []
})
return result
}