Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / rcpp-trycatch.R
Last active Mar 25, 2022
Demonstration of performance penalty of Rcpp code being wrapped in tryCatch
View rcpp-trycatch.R
library(Rcpp)
library(profvis)
cppFunction('
double
call_rnorm(int ignored)
{
Function f("rnorm");
NumericVector x = f(1);
@louisswarren
louisswarren / expandable_list.c
Created Mar 22, 2022
Linked lists can be expanded without invalidating pointers
View expandable_list.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
struct node {
struct node *next;
long v;
};
struct list {
@louisswarren
louisswarren / tm.c
Created Feb 22, 2022
Compressed binary tape idea
View tm.c
#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;
View log.c
#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 Dec 20, 2021
Mersenne primes 2^31-1
View generators.c
#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 / picklejson.py
Last active Nov 18, 2021
Convert between pickle and json sequences
View picklejson.py
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 / dice.c
Created Oct 26, 2021
Roll dice for a while in C
View dice.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 Oct 1, 2021
Generators in C
View generator.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 Aug 28, 2021
3D SVG renderer for menger sponges
View menger.py
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 / pool.c
Created May 11, 2021
Linked list pool
View pool.c
#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;
};