Skip to content

Instantly share code, notes, and snippets.

View dnbaker's full-sized avatar

Daniel Baker dnbaker

View GitHub Profile
@dnbaker
dnbaker / time_code.h
Last active October 21, 2017 17:27
Code Timing Macro (C++)
// You must have #included <chrono> and <cstdio>
#define TIME_CODE(code, name) do \
{ \
using namespace std::chrono; \
const auto i(system_clock::now()); \
{ code } \
const auto j(system_clock::now()); \
std::fprintf(stderr, "Task %s took %lfs\n", name, duration<double>(j - i).count());\
} while(0)
@dnbaker
dnbaker / inthash.c
Created February 3, 2017 02:39 — forked from lh3/inthash.c
Invertible integer hash functions
/*
For any 1<k<=64, let mask=(1<<k)-1. hash_64() is a bijection on [0,1<<k), which means
hash_64(x, mask)==hash_64(y, mask) if and only if x==y. hash_64i() is the inversion of
hash_64(): hash_64i(hash_64(x, mask), mask) == hash_64(hash_64i(x, mask), mask) == x.
*/
// Thomas Wang's integer hash functions. See <https://gist.github.com/lh3/59882d6b96166dfc3d8d> for a snapshot.
uint64_t hash_64(uint64_t key, uint64_t mask)
{
key = (~key + (key << 21)) & mask; // key = (key << 21) - key - 1;
@dnbaker
dnbaker / hmm.tex
Created February 22, 2017 18:58 — forked from mblondel/hmm.tex
Good-looking HMM and Lattice diagrams using TikZ
% (C) Mathieu Blondel, July 2010
\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
@dnbaker
dnbaker / hmm.tex
Created February 22, 2017 18:58 — forked from mblondel/hmm.tex
Good-looking HMM and Lattice diagrams using TikZ
% (C) Mathieu Blondel, July 2010
\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
@dnbaker
dnbaker / cult_of_ignorance.md
Created March 14, 2017 21:55 — forked from conspect/cult_of_ignorance.md
A Cult Of Ignorance, Isaac Asimov

It's hard to quarrel with that ancient justification of the free press: "America's right to know." It seems almost cruel to ask, ingeniously, "America's right to know what, please? Science? Mathematics? Economics? Foreign languages?"

None of those things, of course. In fact, one might well suppose that the popular feeling is that Americans are a lot better off without any of that tripe.

There is a cult of ignorance in the United States, and there always has been. The strain of anti-intellectualism has been a constant thread winding its way throughout political and cultural life, nurtured by the false notion that democracy means that "my ignorance is just as good as your knowledge."

Politicians have routinely striven to speak the language of Shakespeare and Milton as ungrammaticaly as possible in order to avoid offending their audiences by appearing to have gone to school. Thus, Adlai Stevenson, who incautiously allowed intelligence and learning and wit to peep out of his speeches, found the American people

@dnbaker
dnbaker / gen_emoji_dict.py
Created August 12, 2017 21:47
Generate two-way emoji dictionary
#!/usr/bin/env python
import pickle
emoji_dict = {
'\xf0\x9f\x98\x81': 'grinning face with smiling eyes',
'\xf0\x9f\x98\x82': 'face with tears of joy',
'\xf0\x9f\x98\x83': 'smiling face with open mouth',
'\xf0\x9f\x98\x84': 'smiling face with open mouth and smiling eyes',
'\xf0\x9f\x98\x85': 'smiling face with open mouth and cold sweat',
'\xf0\x9f\x98\x86': 'smiling face with open mouth and tightly-closed eyes',
@dnbaker
dnbaker / yes
Last active October 17, 2017 05:04
Very fast yes command (4x as fast as gnu coreutils)
#include <cstdio>
#include <cstring>
#include <string>
#include "unistd.h"
#ifndef _BUF_SZ__
#define _BUF_SZ__ 1 << 16
#endif
int main(int argc, char *argv[]) {
@dnbaker
dnbaker / prnvector.h
Last active October 29, 2017 06:09
PRNVector
#ifndef _PRN_VECTOR_H__
#define _PRN_VECTOR_H__
template<typename RNG>
struct UnchangedRNGDistribution {
auto operator()(RNG &rng) const {return rng();}
void reset() {} // For interface compatibility with generators with internal state.
};
template<typename RNG=std::mt19937_64, typename Distribution=UnchangedRNGDistribution<RNG>>
@dnbaker
dnbaker / 2bit.cpp
Last active January 12, 2018 21:49
Convert fasta file to 2-bit representation
#include <cassert>
#include <fstream>
struct cdata {
uint64_t data;
unsigned count;
cdata() {reset();}
void reset() {data = count = 0;}
template<typename T>
void write(T &stream, size_t nbytes=sizeof(data) / sizeof(char)) {
@dnbaker
dnbaker / bin2dna.c
Last active January 14, 2018 23:36
Convert binary stream into DNA characters
#include <stdio.h>
int main() {
int i, c;
while((c = getc_unlocked(stdin)) != EOF)
for(i = 0; i < 3; fputc("ACGT"[(c >> (i++ << 1)) & 3], stdout));
}