Skip to content

Instantly share code, notes, and snippets.

@attractivechaos
attractivechaos / dlist.h
Created January 19, 2020 02:50
Demonstrating an intrusive doubly linked list
#pragma once // or use the #ifndef guard
#include <stddef.h> // for offsetof()
typedef struct dl_head_s { // this struct can't be hidden
struct dl_head_s *p[2]; // p[0] points the previous record; p[1] points to the next
} dl_head_t;
// Given a pointer to a struct member, get the pointer to the struct
#define dl_container_of(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))
#!/usr/bin/env python
import random
import timeit
import string
import sys
import seqpy
global complement
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
@attractivechaos
attractivechaos / getopt.c
Created February 18, 2018 13:28
Implementation of getopt() and getopt_long() from musl
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "getopt.h"
char *optarg;
int optind=1, opterr=1, optopt, __optpos, optreset=0;
#define optpos __optpos
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#if HAVE_CILK
#include <cilk/cilk.h>
#include <cilk/cilk_api.h>
#endif
typedef struct {
int max_iter, w, h;
@attractivechaos
attractivechaos / rsort.c
Created June 7, 2012 04:58
Fast radix sort
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define rstype_t uint64_t // type of the array
#define rskey(x) (x) // specify how to get the integer from rstype_t
#define RS_MIN_SIZE 64 // for an array smaller than this, use insertion sort
typedef struct {
@attractivechaos
attractivechaos / get_votes.pl
Created March 24, 2012 17:15
Plot HackerNews polls on favorite and disliked programming languages
#!/usr/bin/env perl
# This script collects voting from HackerNews and outputs a plot votes.eps
# You need to have gnuplot installed for plotting.
use strict;
use warnings;
use IO::Socket::INET;
sub http_get {
@attractivechaos
attractivechaos / compile-d.mak
Created February 16, 2012 22:15
Compile the D compiler
MODEL=64
PROTO=git
all:bin/dmd.conf bin/rdmd
@echo "Now add the following to .bashrc: export PATH=\"`pwd`/bin:"'$$PATH"'
# Compile the D compiler
dmd/src/posix.mak:
git clone $(PROTO)://github.com/D-Programming-Language/dmd.git