Skip to content

Instantly share code, notes, and snippets.

@igorburago
igorburago / partsort.c
Last active June 6, 2024 07:00
Fast partial sorting of an array on a subinterval
/* Copyright 2024 Igor Burago. Distributed under the ISC License terms. */
#include <math.h> /* for partition_floyd_rivest() */
#include <stddef.h> /* ptrdiff_t */
#include <stdint.h>
#include <string.h> /* memcpy(), memset() */
/* Built-in u128 is not strictly required, but it makes mcg64 simpler and faster. */
typedef __uint128_t uint128_t;
commit 989ecf2db1b2c5aaa21299e7c78108eb29935e11
Author: Marius Eriksen <meriksen@fb.com>
Date: Tue Jan 4 12:29:12 2022 -0800
acme: acmesrv
diff --git a/src/cmd/acme/acme.c b/src/cmd/acme/acme.c
index d001a2a8..2ebe5d3d 100644
--- a/src/cmd/acme/acme.c
+++ b/src/cmd/acme/acme.c
@pervognsen
pervognsen / shift_dfa.md
Last active January 27, 2024 19:54
Shift-based DFAs

A traditional table-based DFA implementation looks like this:

uint8_t table[NUM_STATES][256]

uint8_t run(const uint8_t *start, const uint8_t *end, uint8_t state) {
    for (const uint8_t *s = start; s != end; s++)
        state = table[state][*s];
    return state;
}
@ityonemo
ityonemo / test.md
Last active June 7, 2024 13:04
Zig in 30 minutes

A half-hour to learn Zig

This is inspired by https://fasterthanli.me/blog/2020/a-half-hour-to-learn-rust/

Basics

the command zig run my_code.zig will compile and immediately run your Zig program. Each of these cells contains a zig program that you can try to run (some of them contain compile-time errors that you can comment out to play with)

# Hash, displace, and compress: http://cmph.sourceforge.net/papers/esa09.pdf
# This is expected linear time for any seeded hash function that acts like a random hash function (universality isn't enough).
# (Actually, the code as written is O(n log n) when targeting 100% load. It's O(n) when targeting any smaller load factor.)
# You can make keys_per_bucket higher than the default of 4 but construction time will start to increase dramatically.
# The paper this is based on compresses the seeds (so the fact that the algorithm tries seeds in increasing order is important)
# which brings the representation size close to the information-theoretical minimum. I don't do any of that here, but it could
# be done as a postprocess.
def make_perfect_hash(keys, load_factor=1.0, keys_per_bucket=4, rhash=murmurhash, max_seed=1000000):
m = int(len(keys) / load_factor)
r = int(len(keys) / keys_per_bucket)
@katef
katef / hist.awk
Last active October 23, 2020 10:10
#!/usr/bin/awk -f
function strrep(n, c) {
s = ""
for (i = 0; i < n; i++) {
s = c s
}
return s
#!/usr/bin/awk -f
# This program is a copy of guff, a plot device. https://github.com/silentbicycle/guff
# My copy here is written in awk instead of C, has no compelling benefit.
# Public domain. @thingskatedid
# Run as awk -v x=xyz ... or env variables for stuff?
# Assumptions: the data is evenly spaced along the x-axis
# TODO: moving average
@mblondel
mblondel / check_convex.py
Last active March 21, 2022 22:25
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@zbjornson
zbjornson / accurate.cc
Last active December 21, 2022 17:32
Code for fast, accurate float summation
#include <cstddef>
#include <cmath>
#ifdef __FAST_MATH__
#error Compensated summation is unsafe with -ffast-math (/fp:fast)
#endif
inline static void kadd(double& sum, double& c, double y) {
y -= c;
auto t = sum + y;
c = (t - sum) - y;
@mkhl
mkhl / acme-autoformat
Last active July 19, 2023 15:52
My current autoacme event handler script
#!/usr/local/plan9/bin/rc
. 9.rc
. $PLAN9/lib/acme.rc
event=$1
target=$2
fn hashbang {
awk '