Skip to content

Instantly share code, notes, and snippets.

@simonster
simonster / divinverse.jl
Last active August 29, 2015 14:06
Faster vectorized integer division
import Base.rem, Base.div, Base.divrem
unsigned_type(::Int8) = Uint8
unsigned_type(::Int16) = Uint16
unsigned_type(::Int32) = Uint32
unsigned_type(::Int64) = Uint64
unsigned_type(::Int128) = Uint128
immutable SignedMultiplicativeInverse{T<:Signed}
divisor::T
multiplier::T
@simonster
simonster / alist.jl
Last active August 29, 2015 14:07
alist
immutable AListWrapper{Name}; end
immutable AList{Keys<:(AListWrapper...),Types} <: Associative
v::Types
end
macro alist(args...)
keys = Array(Any, length(args))
values = Array(Any, length(args))
for i = 1:length(args)
if !Base.Meta.isexpr(args[i], :(=>))
@simonster
simonster / gist:b1b4cc2ad0daa8e20a99
Last active August 29, 2015 14:11
missingness sum benchmark
immutable CheapMulBool
x::Bool
end
immutable Negated{T}
x::T
end
immutable BitWrapper
x::Vector{Uint64}
end
Base.getindex(x::BitWrapper, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x, i))
if (platform == "win") {
libc = ctypes.open("Ws2_32.dll");
var dnsapi = ctypes.open("Dnsapi.dll");
var DNS_RECORD = new ctypes.StructType("DNS_RECORD");
DNS_RECORD.define([{"pNext":DNS_RECORD.ptr}, {"pName":ctypes.char.ptr}, {"wType":ctypes.unsigned_short},
{"wDataLength":ctypes.unsigned_short}, {"DW":ctypes.unsigned_long}, {"dwTtl":ctypes.unsigned_long},
{"dwReserved":ctypes.unsigned_long}, {"pNameHost":ctypes.char.ptr}]);
var DnsQuery = dnsapi.declare("DnsQuery_A", ctypes.winapi_abi, ctypes.int, ctypes.char.ptr, ctypes.unsigned_short,
ctypes.unsigned_long, ctypes.voidptr_t, DNS_RECORD.ptr, ctypes.voidptr_t);
@simonster
simonster / CSL JSON.js
Created December 6, 2011 08:01
CSL JSON translator (WIP)
{
"translatorID": "bc03b4fe-436d-4a1f-ba59-de4d2d7a63f7",
"label": "CSL JSON",
"creator": "Simon Kornblith",
"target": "json",
"minVersion": "3.0b3",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 3,
@simonster
simonster / gist:1579282
Created January 8, 2012 18:45
Making a new custom document property
// PY-APPSCRIPT
self.asApp.make(new=k.custom_document_property, at=self.asDoc, with_properties={k.name:docPropertyName, k.value:docPropertyValue})
// SCRIPTING BRIDGE
NSAppleEventDescriptor *rd = [NSAppleEventDescriptor recordDescriptor];
[rd setDescriptor:[NSAppleEventDescriptor descriptorWithString:currentPropertyName] forKeyword:'pnam'];
[rd setDescriptor:[NSAppleEventDescriptor descriptorWithString:currentPropertyValue] forKeyword:'DPVu'];
[doc->sbApp sendEvent:'core' id:'crel' parameters:'kocl', @"mCDP", 'insh', doc->sbDoc, 'prdt', rd];
@simonster
simonster / arcs.py
Created April 4, 2012 04:43
Arcs in PostScript
#!/usr/bin/python
LINE_WIDTH = 2;
CIRCLE_RADIUS = 6
IMG_SIZE = 16;
NUM_ARCS = 20;
import subprocess;
p = subprocess.Popen(["gs", "-dSAFER", "-dNOPAUSE", "-r72", "-sDEVICE=pngalpha",
"-sOutputFile=arcs.png", "-"], stdin=subprocess.PIPE);
@simonster
simonster / gist:2920373
Created June 12, 2012 21:56
Asynchronous bottom-up merge sort in JavaScript
var AsyncComparator = function(list) {
this.listsAtCurrentLevel = [];
this.listsAtNextLevel = [];
this.listMerged = [];
for(var i=0; i<list.length; i++) {
this.listsAtCurrentLevel[i] = [list[i]];
}
};
AsyncComparator.prototype.getA = function() {
@simonster
simonster / gist:4712293
Created February 5, 2013 05:05
Modified RDF attachment export example
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:res="http://purl.org/vocab/resourcelist/schema#"
xmlns:z="http://www.zotero.org/namespaces/export#"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:address="http://schemas.talis.com/2005/address/schema#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:bibo="http://purl.org/ontology/bibo/"
xmlns:link="http://purl.org/rss/1.0/modules/link/">
<z:UserItem rdf:about="http://zotero.org/users/67180/items/4BUASI6R">
@simonster
simonster / gist:5864205
Last active December 18, 2015 23:49
JLD mmap benchmark
using HDF5, JLD
const MATRIX_SIZE = 1000
# Write test file
x = rand(MATRIX_SIZE, MATRIX_SIZE)
jld = jldopen("test.jld", "w")
write(jld, "x", x)
close(jld)
macro s()