Skip to content

Instantly share code, notes, and snippets.

View louisswarren's full-sized avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / log.c
Created December 20, 2021 08:51
Integer logs
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define PRIME ((1ul << 31) - 1)
#define GEN ((1ul << 11) - 1)
unsigned
fastlog(uint64_t x)
{
@louisswarren
louisswarren / generators.c
Created December 20, 2021 03:10
Mersenne primes 2^31-1
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define PRIME ((1ull << 31) - 1)
uint32_t
cycle(uint32_t seed, uint32_t prev)
{
uint64_t x = prev;
@louisswarren
louisswarren / 110.svg
Last active November 30, 2021 04:16
Cellular automata
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@louisswarren
louisswarren / picklejson.py
Last active November 18, 2021 23:53
Convert between pickle and json sequences
import sys
import pickle
import json
def usage():
print("Usage:")
print(f"\t{sys.argv[0]} --tojson [PICKLE FILES]")
print(f"\t{sys.argv[0]} --topickle [JSON FILES]")
JD = json.JSONDecoder()
@louisswarren
louisswarren / stdio_redirect.py
Last active November 5, 2021 06:35
Redirect stdout to a file in python using with block
import sys
class stdio_redirect:
def __init__(self, stdin=None, stdout=None, stderr=None):
self.stdin, self.sys_stdin = stdin, sys.stdin
self.stdout, self.sys_stdout = stdout, sys.stdout
self.stderr, self.sys_stderr = stderr, sys.stderr
def __enter__(self):
sys.stdin = self.stdin
@louisswarren
louisswarren / dice.c
Created October 26, 2021 03:10
Roll dice for a while in C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned long long counts[6] = {0};
unsigned long long trials = 0;
void
roll_dice(void)
{
@louisswarren
louisswarren / generator.c
Last active October 1, 2021 00:58
Generators in C
#include <stdio.h>
#define FIRST(N, ...) (N)
#define foreach(X, F, ...) \
for (init_##F(__VA_ARGS__), X = F(FIRST(__VA_ARGS__));\
FIRST(__VA_ARGS__)->_running;\
X = F(FIRST(__VA_ARGS__)))
struct count_state {
int _running;
@louisswarren
louisswarren / menger.py
Last active August 28, 2021 01:01
3D SVG renderer for menger sponges
import numpy as np
from svg import *
compose = lambda f: lambda g: lambda *a, **k: f(g(*a, **k))
PURPLE = '#6a1eb0'
ORANGE = '#ff824a'
BLUE = '#75c1ff'
VP_WIDTH = 2.0
@louisswarren
louisswarren / ffmpeg.md
Last active May 26, 2021 20:52
ffmpeg notes

ffmpeg notes

I often want to do something that I believe is non-trivial in ffmpeg, only to discover that it is actually very simple, if only I had known how to do it. I even end up writing wrappers to do simple things, because I keep forgetting what to do. However, this file is my new solution: simply write my own documentation for things I would occasionally like to do.

@louisswarren
louisswarren / pool.c
Created May 11, 2021 10:18
Linked list pool
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define POOL_NODE_HEADERS struct pool_node *prev, *next
struct pool_node {
POOL_NODE_HEADERS;
};