Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / .gitignore
Last active Aug 12, 2022
CPython testing
View .gitignore
primesmodule.o
primes.*.so
@louisswarren
louisswarren / private_ips.py
Last active Jul 9, 2022
All private IPv4 addresses, in all formats
View private_ips.py
IPV4_OCTET_TYPES = {
4: (24, 16, 8, 0,),
3: (24, 16, 0,),
2: (24, 0,),
1: ( 0,),
}
def num(ip):
octets = tuple(map(int, ip.split('.')))
bases = IPV4_OCTET_TYPES[len(octets)]
@louisswarren
louisswarren / Makefile
Last active Jul 2, 2022
Hash table in C
View Makefile
.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 Jun 29, 2022
Flexible array members in flexible array members are allowed
View flexible-in-flexible.c
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a {
char x;
char buf[];
};
@louisswarren
louisswarren / test.c
Created Jun 26, 2022
Simple testing in C
View test.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 / pre-commit.R.bash
Created Jun 2, 2022
Precommit for linting R
View pre-commit.R.bash
#!/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 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)
{