Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View cls's full-sized avatar

Connor Lane Smith cls

View GitHub Profile
@cls
cls / row_align.c
Last active March 18, 2018 16:53
Translate raw two-dimensional bitmap into word-aligned rows
#include <stdint.h>
#include <string.h>
void
row_align(uint8_t *dst, const uint8_t *src, const size_t width_in_bits, const size_t height)
{
const size_t width_in_whole_bytes = width_in_bits / 8;
if (width_in_bits % 32 == 0) { // word-aligned
memcpy(dst, src, width_in_whole_bytes * height);
@cls
cls / functional-templates.hpp
Last active March 9, 2018 00:44
Type-level linked lists, with map, filter, and foldr
#include <type_traits>
namespace type_list {
class nil {};
template<class Head, class Tail>
class cons {};
// map f [] = []
@cls
cls / suffix-array.c
Last active December 30, 2017 21:17
Constructing a simple suffix array
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int pstrcmp(const void *, const void *);
int
main(void)
{
char buf[BUFSIZ];
@cls
cls / debruijn.hs
Last active October 21, 2017 12:22
Untyped lambda calculus with De Bruijn indices as a nested data type
import Control.Applicative (liftA2)
data Term a = App (Term a) (Term a)
| Lam (Term (Maybe a))
| Var a
instance Functor Term where
fmap f (App s t) = App (fmap f s) (fmap f t)
fmap f (Lam t) = Lam (fmap (fmap f) t)
fmap f (Var x) = Var (f x)
@cls
cls / utf8base64.c
Last active August 7, 2017 22:03
Print non-ASCII UTF-8 sequences' unique Base64 identifiers
#include <stdio.h>
const char clo[64] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6,
};
const char b64[64] = {
@cls
cls / ucd.awk
Last active July 13, 2017 11:35
Script to extract certain fields from the Unicode Character Database
BEGIN { FS = ";"; OFS = ","; }
{ name = $2; }
name ~ /<.*>/ { name = $11; }
name { print $1, name, $3; }
@cls
cls / cartesian.hs
Created February 21, 2017 11:09
Cartesian product over lists
cartesian :: [[a]] -> [[a]]
cartesian [] = [[]]
cartesian (xs:zss) = [x:ys | x <- xs, ys <- cartesian zss]
@cls
cls / catalan.py
Created February 20, 2017 14:45
Calculating the Catalan number series
from fractions import Fraction
def catalan(n):
c = 1
for k in range(2, n + 1):
c *= Fraction(n + k, k)
return int(c)
@cls
cls / repack.py
Last active February 16, 2017 17:42
Repacking structs to minimise bit wastage
from queue import PriorityQueue
class Field:
def __init__(self, size, align):
self.size = size
self.align = align
class Struct:
def __init__(self, fields):
size = 0
@cls
cls / next.sh
Last active May 27, 2016 12:24
Script to execute the next binary along in $PATH
#!/bin/sh
dir=${1%/*}
bin=${1##*/}
shift
unset flag
set -f
IFS=: