Skip to content

Instantly share code, notes, and snippets.

View louisswarren's full-sized avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / Makefile
Last active July 2, 2022 00:30
Hash table in C
.PHONY: default
default: dict
./dict
dict: dict.c set.o
test: test.c set.o
set.o: set.c set.h
@louisswarren
louisswarren / flexible-in-flexible.c
Created June 29, 2022 07:34
Flexible array members in flexible array members are allowed
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a {
char x;
char buf[];
};
@louisswarren
louisswarren / test.c
Created June 26, 2022 03:50
Simple testing in C
int
check(const char *s, int p)
{
static int passes = 0;
static int failures = 0;
const char *fail = "\x1B[31mFAIL\x1B[0m";
const char *pass = "\x1B[34mPASS\x1B[0m";
if (s) {
fprintf(stderr, "[%s] %s\n", p ? pass : fail, s);
@louisswarren
louisswarren / testing.py
Last active December 28, 2022 05:40
Testing using decorators
import io
import sys
class wrap_stdio:
def __init__(self, io_in = ''):
self.io_in = io.StringIO(io_in)
self.io_out = io.StringIO()
self.stdin = sys.stdin
self.stdout = sys.stdout
@louisswarren
louisswarren / pre-commit.R.bash
Created June 2, 2022 02:15
Precommit for linting R
#!/bin/bash
#
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
@louisswarren
louisswarren / rcpp-trycatch.R
Last active March 25, 2022 00:50
Demonstration of performance penalty of Rcpp code being wrapped in tryCatch
library(Rcpp)
library(profvis)
cppFunction('
double
call_rnorm(int ignored)
{
Function f("rnorm");
NumericVector x = f(1);
@louisswarren
louisswarren / expandable_list.c
Created March 22, 2022 08:56
Linked lists can be expanded without invalidating pointers
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
struct node {
struct node *next;
long v;
};
struct list {
@louisswarren
louisswarren / tm.c
Created February 22, 2022 08:32
Compressed binary tape idea
#include <stdio.h>
#include <stdint.h>
#define BASE_SIZE 8
struct cell {
uint64_t value : 1;
uint64_t reps : 17;
uint64_t prev : 23;
uint64_t next : 23;
@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;