Skip to content

Instantly share code, notes, and snippets.

@williame
williame / gist:3fcfeb1d424f01b2fa59
Created June 13, 2014 07:43
iterating over the set bits in an int efficiently
for(uint32_t m = mask, i = ~0U; m; ) {
auto shift = __builtin_ffs(m);
m >>= shift;
i += shift;
... do something with i
}
@williame
williame / gist:482fee74d096e47e006b
Last active August 29, 2015 14:02
benchmark iterating over set bits in a uint32_t
#include <cinttypes>
#include <cstdlib>
#include <ctime>
#include <iostream>
int main(int argc, char** args) {
srand(time(NULL));
import os, sys
from PIL import Image
size = 128, 128
w, h = size
reds = [0] * w * h
greens = [0] * w * h
blues = [0] * w * h
count = 0
@williame
williame / gist:935a47a6262fc5023847
Last active August 29, 2015 14:05
swap patterns of mosaic rotations placement
XX.XX@XXX.XXX@@.XX..XXX..X.XX....X@.X.X...XXX......X.X@.@@.X@.XX......X...@....@X.X.X.X@@..X....@.X@.........@X.X.@.@@X.@.X.@...X..X.@@X...X.X.X....@X@XX.....XX.X.@X......X..X@..@@.X...@XXX@.@@XXX.XXXXXXX.X..X@XX.X@X.@@@..X.@...XX..X@XXX@X..XX.@..@.@@X.@..X.X..XX....X@@...X.XXXX.....X@...@..@...X@X@X.XXXX.@X..@....@......X...X@.....@@..X@X@..X....@@.X..@.@@XXX.X.....XX@X.@...@XX..@X@....XX..X.@.@X@X.XX...X.@..@..@@X.XXX..@X.XX..X....@@.X@...XX@X........X.@.XX.XX.X..@X@X.......X.XX@..X..XX.X@.....X.X@.@X.@@...X.@@.XX..@X.X@X@X@@@.XXX@.@.XX....X....@...X@@@X.@.@.@..X@.X.XX@@.X.X.@.@X.X..XXX.@.X..@XX.XX@XXX....X@.XXX.X.XX@X@.X..@X...X..X@X.@XX...X....@@.XXX...XXXXX...XX..X.X.X.@X@XX.XX.@X.XXX...@.X.@XX@X@X...@X...@.XXX...@X.@XX..XX..X..@.@X....X.X@.X@X.X@.@.@X....@@..@.X.X.@.@.X@.X@...@......@@X.XX..@X.XXXX.X.X.XX@.XX..XX.XXX.@@.X...X.X..X.@@..X@....@..XX@X....@X...@X..X.X...XX..@XX@X.XX.@........X.X...X@@@X@@.@.X..X.X.X..X.@XXX@XX.X@....X.X@XX.X.@.@...@@@.@X...@@XX@..@......@X@...X.X.@..@.@X....@X....@.X@.@.X@.
import urllib, re, os, urlparse, json, random, sys, getopt, gzip, math, time
from multiprocessing.dummy import Pool as ThreadPool
from cStringIO import StringIO
from PIL import Image
import numpy as np
def download_all_thumbs(ld_num,dest_folder=None):
event_name = 'ludum-dare-%d' % ld_num
entries_page_url_template = "http://www.ludumdare.com/compo/%s/?action=preview&etype=&start=%%d" % event_name
@williame
williame / diff1.py
Created November 18, 2010 09:35
zip to folder delta calc
to_mem = True # set to False if you want a actual zip file of the delta made (e.g. you want to recompress it with 7z)
import zipfile
import sys, os, cStringIO
if len(sys.argv) != 3:
print "Diffs a zip file against a folder, and computes a file-granularity delta"
print "usage: python diff1.py prev.zip newfolder"
print "e.g. is MRise_1.0.zip is in your current directory, and you have the 1.6 directory tree in a folder called MRise_1.6, then:"
print " python diff1.py MRise_1.0.zip MRise_1.6"
@williame
williame / dep.py
Created November 25, 2010 11:56
A simple attempt at working out what files are in and out of a mod, and if its broken
import os, sys, string
import xml.dom.minidom as minidom
from struct import unpack
from itertools import chain
class File:
"""a file (type and path)"""
MAP = "map"
SCENARIO = "scenario"
=== Mod check failed ===
(model BROKEN techs/megapack/factions/persian/units/swordman/models/guard_attacking.g3d (referenced by: unit ../swordman.xml)) ('Error reading G3D file', IOError(2, 'No such file or directory')), ('File does not exist',)
(model BROKEN techs/megapack/factions/tech/units/swordman/models/guard_attacking.g3d (referenced by: unit ../swordman.xml)) ('Error reading G3D file', IOError(2, 'No such file or directory')), ('File does not exist',)
(model BROKEN techs/vbros_pack_1/factions/canadians/units/beaver/models/worker_standing_loaded_wood.g3d (referenced by: unit ../beaver.xml)) ('Error reading G3D file', IOError(2, 'No such file or directory')), ('File does not exist',)
(model BROKEN techs/vbros_pack_1/factions/canadians/units/inuit_man/models/minstrel_summoning.g3d (referenced by: unit ../inuit_man.xml)) ('Error reading G3D file', IOError(2, 'No such file or directory')), ('File does not exist',)
(particle BROKEN techs/megapack/factions/persian/units/worker/particle_splash.xml (referenced
@williame
williame / glest_mod_pack.py
Created November 26, 2010 13:09
a mod packing tool for Glest, MegaGlest and GAE
# glest_mod_pack is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
@williame
williame / test_tornado_async_error.py
Created May 31, 2011 07:28
For me at least, raising an exception in a callback does not do anything to the request
from tornado import ioloop, web, httpserver, httpclient
PORT = 8080
class TestHandler(web.RequestHandler):
@web.asynchronous
def get(self):
url = "http://www.google.com"
print "fetching",url