Skip to content

Instantly share code, notes, and snippets.

View 6502's full-sized avatar

Andrea Griffini 6502

View GitHub Profile
import sys
L0 = sys.argv[1]
N = int(sys.argv[2])
alpha = set("abcdefghijklmnopqrstuvwxyz")
words = set()
for L in open("words_alpha.txt"):
L = L.strip()
@6502
6502 / createElement.js
Created October 24, 2016 08:41
Utility to create a DOM element with prescribed style, parent and content (e.g. x=e("div#mydiv.myclass", {s_border:"solid 1px #000"}, "textnode", e("#inner")))
function e(type, opts) {
var i;
if (!opts) opts = {};
i = type.indexOf(".");
if (i >= 0) { opts.className = type.slice(i+1); type = type.slice(0, i); }
i = type.indexOf("#");
if (i >= 0) { opts.id = type.slice(i+1); type = type.slice(0, i); }
var d = document.createElement(type||"div");
if (opts.className) d.className = opts.className;
if (opts.id) d.id = opts.id;
@6502
6502 / log.h
Created October 23, 2016 21:12
A simple logging facility
#if !defined(LOG_H_INCLUDED)
#define LOG_H_INCLUDED
#include <stdio.h>
#include <vector>
#include <string>
#include <deque>
#include <map>
#include <functional>
#include <time.h>
@6502
6502 / refcount.h
Created October 23, 2016 21:09
A simple intrusive reference counter approach (referenced instances must have a `refcount` integer but don't need to derive from `RefCounted`)
#if !defined(REFCOUNT_H_INCLUDED)
#define REFCOUNT_H_INCLUDED
#include <atomic>
#include <algorithm>
struct RefCounted {
std::atomic_int refcount;
RefCounted() : refcount(0) {}
};
@6502
6502 / stringf.cpp
Last active October 23, 2016 20:35
Reasonable string formatting for C++ (requires C++11)
#include <stdarg.c>
#include <string>
#include <vector>
std::string stringf(const char *fmt, ...) {
std::vector<char> buffer(256);
va_list args, cp;
va_start(args, fmt);
va_copy(cp, args);
int sz = vsnprintf(&buffer[0], buffer.size(), fmt, args);
@6502
6502 / gist:8242e70a751a7f0a41fc
Created August 3, 2014 06:45
Stripping whitespace and c-style comments from otherwise valid JSON data
function strip_comments(s) {
return s.replace(/[ \t\r\n]+|\/\*(?:[^*]|[*][^\/])*\*\/|\/\/[^\n]*\n|"(?:[^\\"]|\\.)*"|[^ \t\r\n"\/]+/g,
function(s){ return (" \t\r\n\/".indexOf(s[0])==-1) ? s : ""; });
}