Skip to content

Instantly share code, notes, and snippets.

@1995eaton
1995eaton / prime.c
Created April 27, 2016 13:29
Miller-Rabin primality test using GMP in C99
#include <stdio.h>
#include <gmp.h>
unsigned urandom(void) {
unsigned r;
FILE *fp = fopen("/dev/urandom", "r");
fread((void *)&r, sizeof r, 1, fp);
fclose(fp);
return r;
}
@1995eaton
1995eaton / background.js
Created July 30, 2014 21:22
A simple Chrome extension that opens random links
// A background script is needed in order to store
// persistent objects in memory. Content scripts
// run a seperate instance for each open tab in Chrome.
var nextLink = (function(linkArray) {
// Initialize a copy of the original links
var currentSet = linkArray.slice();
return function() {
@1995eaton
1995eaton / readin.c
Created November 8, 2014 23:29
C stdin reader example with dynamic memory allocation
#include <stdio.h>
#include <stdlib.h>
static char *
read_stdin (void)
{
size_t cap = 4096, /* Initial capacity for the char buffer */
len = 0; /* Current offset of the buffer */
char *buffer = malloc(cap * sizeof (char));
int c;
@1995eaton
1995eaton / vec.c
Created April 17, 2015 19:27
C macro vector library
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define LAMBDA(RETURN_TYPE, BODY) ({ RETURN_TYPE __fn__ BODY __fn__; })
#define __NARG__(...) __NARG_I_(__VA_ARGS__,__RSEQ_N())
#define __NARG_I_(...) __ARG_N(__VA_ARGS__)
#define __ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
@1995eaton
1995eaton / mt.h
Created October 17, 2015 23:57
Mersenne Twister (32 and 64 bit) C89+ header
#ifndef MT_H
#define MT_H
#define MT_GEN(TYPE, NAME, W, N, M, A, U, D, S, B, T, C, F, L, LM, UM) \
typedef struct { \
TYPE MT[N]; \
TYPE index; \
} NAME; \
void NAME##_seed(NAME *mt, TYPE seed) { \
mt->index = (N); \
@1995eaton
1995eaton / create_bootable_drive.txt
Created September 10, 2022 18:51
Create a bootable drive from an ISO in Windows
cmd:
disk part
list disk
select disk X
clean
create partition primary
select partition 1
format fs=ntfs quick
active
exit
@1995eaton
1995eaton / regex_utils.cc
Created April 21, 2015 04:46
Regex Utils for C++11
#include <iostream>
#include <algorithm>
#include <vector>
#include <regex>
std::regex::flag_type parse_flags(const std::string& flag_str) {
std::regex::flag_type grammar = std::regex::ECMAScript;
std::regex::flag_type flags = grammar ^ grammar;
for (const auto& c: flag_str) {
switch (c) {
@1995eaton
1995eaton / client.py
Created October 13, 2014 17:47
Simple Python Socket Messenger
#!/usr/bin/python2
from threading import Thread
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9999,))
class ReplyHandler(Thread):
@1995eaton
1995eaton / huffman.js
Created August 4, 2014 04:04
Huffman coding in JavaScript
log = console.log.bind(console);
var Heap = function(fn) {
this.fn = fn || function(e) {
return e;
};
this.items = [];
};
Heap.prototype = {
@1995eaton
1995eaton / png.c
Created August 23, 2014 04:42
A simple wrapper for libpng
/* gcc -O2 -Werror -I/usr/include/libpng16 -lpng png.c -o png */
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
typedef struct PNG {
int width;
int height;
int depth;