Skip to content

Instantly share code, notes, and snippets.

View gburd's full-sized avatar

Greg Burd gburd

View GitHub Profile
@gburd
gburd / duration.h
Last active January 16, 2025 01:58
Measure the duration of things in C using either the real-time high-resolution nanosecond clock or CPU clock ticks via the RDTSC instruction.
/*
* Copyright (C) 2013, all rights reserved by Gregory Burd <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* version 2 (MPLv2). If a copy of the MPL was not distributed with this file,
* you can obtain one at: http://mozilla.org/MPL/2.0/
*
* NOTES:
* - on some platforms this will require -lrt
*/
@gburd
gburd / fifo_q.h
Last active December 1, 2023 13:50
A (well tested) ANSI C macro-based implementation of a bounded FIFO Queue. h/t QuviQ
/*
* fifo_q: a macro-based implementation of a FIFO Queue
*
* Copyright (c) 2012 Basho Technologies, Inc. All Rights Reserved.
* Author: Gregory Burd <[email protected]> <[email protected]>
*
* This file is provided to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain
* a copy of the License at
@gburd
gburd / roman_numerals.erl
Last active December 17, 2015 03:08
Given any number output the roman numeral representation.
-module(roman_numerals).
-compile(export_all).
% dec_to_roman/1 : integer -> string
% format the integer num using roman numerals
decimal_to_roman(Num) ->
decimal_to_roman(Num, "",
[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1],
["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]).
@gburd
gburd / brew --config
Last active December 22, 2015 16:39
Homebrew: pure failed to build on OS X 10.9 (13A569)
HOMEBREW_VERSION: 0.9.4
ORIGIN: https://github.com/mxcl/homebrew
HEAD: edce4ed1223e34b7d4a68eebebc417cb0462ca7d
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
CPU: 8-core 64-bit sandybridge
OS X: 10.9-x86_64
Xcode: 5.0 => /Applications/Xcode5-DP5.app/Contents/Developer
GCC-4.2: build 5666
LLVM-GCC: N/A
@gburd
gburd / extract
Created September 10, 2013 13:07
A helpful alias for extracting compressed/archived files within a bash/sh environment.
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 && cd $(basename "$1" .tar.bz2) ;;
*.tar.gz) tar xvzf $1 && cd $(basename "$1" .tar.gz) ;;
*.tar.xz) tar Jxvf $1 && cd $(basename "$1" .tar.xz) ;;
*.bz2) bunzip2 $1 && cd $(basename "$1" /bz2) ;;
*.rar) unrar x $1 && cd $(basename "$1" .rar) ;;
*.gz) gunzip $1 && cd $(basename "$1" .gz) ;;
*.tar) tar xvf $1 && cd $(basename "$1" .tar) ;;
@gburd
gburd / gist:7101474
Created October 22, 2013 14:13
Morton encoding/decoding through bit interleaving, or how to interleave the bits of n-integers of m size. See also: * Morton Numbers * Morton Keys * Z-Order Curve Goal: efficient UB-Tree indexing
http://en.wikipedia.org/wiki/Z-order_curve
http://stackoverflow.com/questions/18529057/produce-interleaving-bit-patterns-morton-keys-for-32-bit-64-bit-and-128bit
http://stackoverflow.com/questions/1024754/how-to-compute-a-3d-morton-number-interleave-the-bits-of-3-ints
http://code.activestate.com/recipes/577558-interleave-bits-aka-morton-ize-aka-z-order-curve/
http://www.forceflow.be/2013/10/07/morton-encodingdecoding-through-bit-interleaving-implementations/
https://www.fpcomplete.com/user/edwardk/revisiting-matrix-multiplication/part-1
https://www.fpcomplete.com/user/edwardk/revisiting-matrix-multiplication/part-2
It turns out we don't have to actually interleave the bits if all we want is the ability to compare two keys as if they had been interleaved. What we need to know is where the most significant difference between them occurs. Given two halves of a key we can exclusive or them together to find the positions at which they differ.

Keybase proof

I hereby claim:

  • I am gburd on github.
  • I am gregburd (https://keybase.io/gregburd) on keybase.
  • I have a public key whose fingerprint is D4BB 42BE 729A EFBD 2EFE BF88 2293 1AF7 895E 82DF

To claim this, I am signing this object:

@gburd
gburd / ls-mem.sh
Created December 16, 2015 16:00
List memory usage of all running processes ordered by amount used (Linux-specific)
#!/bin/bash
ps -A --sort -rss -o comm,pmem,rss | awk '
NR == 1 { print; next }
{ a[$1] += $2; b[$1] += $3; }
END {
for (i in a) {
size_in_bytes = b[i] * 1024
split("B KB MB GB TB PB", unit)
human_readable = 0
if (size_in_bytes == 0) {
@gburd
gburd / Makefile
Created January 12, 2016 20:47 — forked from postmodern/Makefile
A generic Makefile for building/signing/install bash scripts
NAME=project
VERSION=0.0.1
DIRS=etc lib bin sbin share
INSTALL_DIRS=`find $(DIRS) -type d 2>/dev/null`
INSTALL_FILES=`find $(DIRS) -type f 2>/dev/null`
DOC_FILES=*.md *.txt
PKG_DIR=pkg
PKG_NAME=$(NAME)-$(VERSION)