Skip to content

Instantly share code, notes, and snippets.

@joesavage
joesavage / wsp.py
Created March 17, 2019 15:25
Halldorsson's algorithm (WSP)
from math import sqrt
# Find an approximate solution to the weighted set packing problem using
# Halldorsson's algorithm (2000). Our approximate solution is at worst only a
# factor of sqrt(m) worse than the optimal solution.
#
# Input:
# S = { a, b, c... } # Set of base elements (m = |S|)
# C = { C_1, C_2, C_3, ... } # Collection of weighted subsets of S
# C_i = ({ a, c, ... }, weight) # Weighted subset of S
@joesavage
joesavage / commands.txt
Last active August 11, 2016 06:41
`dwarfdump` on macOS and Linux
=== macOS
$ clang -v
Apple LLVM version 7.0.2 (clang-700.1.81)
<snip>
$ clang hello.c -g -o hello.out
$ dwarfdump hello.out # NOTE: macOS-specific binary that reads Mach-O rather than ELF executables
----------------------------------------------------------------------
File: hello.out (x86_64)
----------------------------------------------------------------------
.debug_info contents:
# /etc/csh.login OR ~/.login
setenv SHELL /bin/bash
exec /bin/bash -l
Background: #202020
Font: Monaco, 11pt
Text: #CFCFCF
Bold Text: #CFCFCF
Selection: #CFCFCF (65% Opacity) / #9C9C9C
Cursor: #CFCFCF
ANSI Colours:
Black (0): #151515
Bright Black (8): #3E3E3E
Red (1): #9A2D32
@joesavage
joesavage / after.js
Created March 15, 2016 08:27
emscripten_suspend web worker test case
self.addEventListener('message', (function(e) {
var data = e.data;
switch (data.cmd) {
case 'init':
(function() {
run();
var desiredInput = "success";
var insertChar = (function(i, self) {
Module.userInput.push(desiredInput.charCodeAt(i));
Module.resume("char_ready");
(function() {
var desiredInput = "success";
var insertChar = (function(i, self) {
userInput.push(desiredInput.charCodeAt(i));
Module.resume("char_ready");
if (i < desiredInput.length) {
return setTimeout((function() { self(i + 1, self) }), Math.floor(Math.random() * 1500));
}
});
insertChar(0, insertChar);
row(0, R, R) :- !.
row(N, R, Acc) :- L is N - 1,
row(L, R, [N | Acc]).
% LESS EFFICIENT VERSION:
% row(0, []) :- !.
% row(N, R) :- L is N - 1,
% row(L, S),
% append(S, [N], R).
@joesavage
joesavage / gist:00945a6ecc4abb8721cf
Last active August 29, 2015 14:15
LLVM [2.6] JIT Tutorial 1 (OS X, 2015)
// A modified version of the "LLVM JIT Tutorial 1" code from http://goo.gl/1cNfH7
// which actually works with the latest versions of LLVM as of Feb 2015.
//
// To compile if you're on OS X using 'llvm' [libs, etc.] from Homebrew:
// clang++ -g test.cpp `/usr/local/opt/llvm/bin/llvm-config --cxxflags --ldflags --libs core --system-libs` -I/usr/local/opt/llvm/include -o test
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/PassManager.h"
#include "llvm/IR/CallingConv.h"
# 1. Generate private key
openssl genrsa -out /etc/ssl/private/example.com.key 4096
# 2. Generate Certificate Signing Request (CSR)
# [Fill in details as appropriate leaving email, challenge password, and optional company name empty. Be careful with FQDN!]
openssl req -new -key /etc/ssl/private/example.com.key -out /etc/ssl/private/example.com.csr
# 3. Submit CSR and recieve site certificate
# 4. Set permissions as appropriate
@joesavage
joesavage / brainfuck.cpp
Created November 17, 2014 19:28
Brainfuck Compiler/Interpreter
// A small, and relatively primitive, C-style Brainfuck compiler/interpreter.
// Written by Joe Savage, 2014
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#define STACK_SIZE 512
#define DATA_SIZE 65535