Skip to content

Instantly share code, notes, and snippets.

@attractivechaos
attractivechaos / matmul-split.c
Created January 3, 2024 14:23
Alternative matmul with N+1 allocations per matrix
// see also: https://github.com/attractivechaos/plb2/blob/master/src/c/matmul.c
#include <stdio.h>
#include <stdlib.h>
double **mat_alloc(int n_row, int n_col)
{
double **mat, *a;
int i;
mat = (double**)malloc(n_row * sizeof(void*));
/* The MIT License
Copyright (c) 2008, 2011 Attractive Chaos <attractor@live.co.uk>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
@attractivechaos
attractivechaos / clist.h
Last active January 19, 2020 05:35
Demonstration of a circular doubly linked list
#pragma once // or use the #ifndef guard
#include <stddef.h> // for offsetof()
typedef struct cl_head_s {
struct cl_head_s *prev, *next;
} cl_head_t;
static inline void cl_insert_after(cl_head_t *q, cl_head_t *p)
{ // insert _p_ after _q_; _q_ is in the list
p->prev = q, p->next = q->next, q->next = p, p->next->prev = p;
@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 {