Skip to content

Instantly share code, notes, and snippets.

View goldsborough's full-sized avatar
🔨
Fixing things

Peter Goldsborough goldsborough

🔨
Fixing things
View GitHub Profile
@goldsborough
goldsborough / benchmark.cpp
Created October 1, 2015 22:32
C++ benchmarking code
template<typename Function, typename... Args>
auto benchmark(const std::size_t runs, Function function, Args&&... args)
{
using duration_t = std::chrono::duration<double, std::milli>;
using clock = std::chrono::high_resolution_clock;
duration_t duration(0);
@goldsborough
goldsborough / gist:29dc6a8234a02d614c85
Created October 5, 2015 16:28
Optimized, colorless Red-Black tree which stores color information in relative node-ordering
template<typename Key, typename Value>
class RedBlackTree
{
public:
using size_t = std::size_t;
RedBlackTree()
: _root(nullptr)
, _size(0)
@goldsborough
goldsborough / memoize.py
Created October 7, 2015 15:33
A python function to memoize DP problems
def memoize(cache_index=0):
def decorator(function):
cache = {}
def proxy(*args, **kwargs):
key = args[cache_index]
if key not in cache:
cache[key] = function(*args, **kwargs)
return cache[key]
return proxy
return decorator
@goldsborough
goldsborough / memoize_many.py
Last active October 8, 2015 21:40
A python function to memoize DP problems with multiple-index keys
def memoize(*indices):
def decorator(function):
cache = {}
def proxy(*args, **kwargs):
key_args = [args[i] for i in indices] if indices else args
key = tuple(key_args)
if key not in cache:
cache[key] = function(*args, **kwargs)
return cache[key]
return proxy
@goldsborough
goldsborough / bit-manipulation.md
Created October 8, 2015 16:08
Notes on Bit-Manipulation problems.

Bit Manipulation

Tricks

A value is a power of 2 if you can AND it with itself - 1 and have the result be 0.

Example: 16

16: 10000 16 - 1: 01111

@goldsborough
goldsborough / gist:7322569079483e2ab043
Created October 8, 2015 21:27
A python script to compute the number of possibilities to parenthesize a boolean expression so that it always evaluates to the same expected value.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
def parse(expression):
pattern = re.compile(r'(\d+)\s*(\W)\s*(\d+)')
values = []
operators = []
@goldsborough
goldsborough / graphs.cpp
Created October 10, 2015 21:29
Graph-processing code in C++.
class Graph
{
public:
using size_t = std::size_t;
using vertex_t = std::size_t;
struct Edge { vertex_t vertex; size_t id; };
@goldsborough
goldsborough / dg_connected.cpp
Created October 10, 2015 22:46
Are two vertices in a directed graph connected?
class DirectedGraph
{
public:
using size_t = std::size_t;
using vertex_t = std::size_t;
using adjacent_t = std::vector<vertex_t>;
template<typename T>
std::size_t find_lsb(T value)
{
T less = value - 1;
value = (less | value) ^ less;
return std::log2(value + 1);
}
@goldsborough
goldsborough / sorted_insert.cpp
Created October 15, 2015 23:10
Insert a sorted range into another sorted range.
class SortedInsert
{
public:
template<typename Iterator1, typename Iterator2>
void operator()(Iterator1 first_begin,
Iterator1 first_end,
Iterator2 second_begin,
Iterator2 second_end)
{