Skip to content

Instantly share code, notes, and snippets.

View ziap's full-sized avatar

Zap ziap

View GitHub Profile
@ziap
ziap / avl.c
Last active August 7, 2024 06:03
AVL tree implementation in C
#include <stdlib.h>
#include <stdint.h>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
typedef enum {
SIDE_LEFT = 0,
SIDE_RIGHT,
SIDE_COUNT,
} AVL_Side;
@ziap
ziap / function.cpp
Last active May 5, 2024 07:54
Fat pointer based polymorphic function wrapper, `std::function` alternative
template<class>
class Function {};
template<class R, class ...Args>
class Function<R(Args...)> {
private:
template<class F>
static R type_erase(const void *fn, Args... args) {
return (*(const F*)fn)(args...);
}
@ziap
ziap / arena.h
Last active June 19, 2024 11:17
Zap's personal approach to arena allocation
/*
arena.h - Zap's personal approach to arena allocation
USAGE
This is an STB-style single-header library.
#define ARENA_IMPLEMENTATION // (in *one* C file)
#include "arena.h"
#define ARENA_ALLOC and ARENA_DEALLOC to avoid using malloc/free and create a
custom backend for the allocator.
@ziap
ziap / event-poll.js
Created March 30, 2024 13:54
Async event polling in JavaScript. Just a proof-of-concept, not recommended for serious development, even though it's pretty convenient.
class Queue {
#start = 0
#end = 0
#len = 0
#cap = 1 << 10
#cap_mask = this.#cap - 1
#data = new Array(this.#cap)
@ziap
ziap / trig_gen.py
Last active March 3, 2024 05:22
Trigonometric functions with angles measured in turn (rad / 2pi)
from mpmath import pi
import mpmath
mpmath.mp.dps = 20
def generate_cos():
weights = [-1, 0, 1, 0]
fac = 1
pow = 1
@ziap
ziap / sqrt.hpp
Last active October 26, 2023 03:09
Sqrt with algorithm for integer for C++
#ifndef SQRT_HPP
#define SQRT_HPP
#include <type_traits>
template <typename T>
constexpr T sqrt(T value) {
if constexpr (std::is_integral_v<T>) {
T op = value;
T res = 0;
@ziap
ziap / safe_eval.js
Created September 29, 2023 04:03
Safe `eval()` in js with dynamic import
async function safe_eval(code) {
const mod_code = `export default () => ${code}`
const mod_blob = new Blob([mod_code], { type: "text/javascript" })
const mod_url = URL.createObjectURL(mod_blob)
const mod = await import(mod_url)
URL.revokeObjectURL(mod_url)
return mod.default()
}
@ziap
ziap / js_printf.h
Last active September 16, 2023 03:59
printf for WASM. Requires std_sprintf <https://github.com/nothings/stb>
#ifndef JS_PRINTF_H
#define JS_PRINTF_H
// This is free and unencumbered software released into the public domain.
// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
// software, either in source code form or as a compiled binary, for any purpose,
// commercial or non-commercial, and by any means.
// In jurisdictions that recognize copyright laws, the author or authors of this
// software dedicate any and all copyright interest in the software to the public
// domain. We make this dedication for the benefit of the public at large and to
@ziap
ziap / grughash.glsl
Last active September 12, 2023 10:35
3D vector hashing function for GLSL
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyright (C) 2023 Zap
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@ziap
ziap / deque-benchmark.js
Last active August 16, 2023 06:36
Benchmark my own deque implementation against invertase/denque
const ITEM = Math.PI
const input = new Array(2000000).fill(ITEM)
Deno.bench({group: "grow", baseline: true}, function deque_grow(timer) {
const deque = new Deque()
for (let i = 0; i < 1000; ++i) deque.pushBack(ITEM)
timer.start()
for (let i = 0; i < 20; ++i) {