Skip to content

Instantly share code, notes, and snippets.

#define CONSTANTHASH_DEPTH 64
// randomly generated constants. The bottom half has to be FFFF or
// else the entire hash loses some strength
static const size_t CONSTANTHASH_CONSTANTS[CONSTANTHASH_DEPTH+1] =
{
0x6157FFFF, 0x5387FFFF, 0x8ECBFFFF, 0xB3DBFFFF, 0x1AFDFFFF, 0xD1FDFFFF, 0x19B3FFFF, 0xE6C7FFFF,
0x53BDFFFF, 0xCDAFFFFF, 0xE543FFFF, 0x369DFFFF, 0x8135FFFF, 0x50E1FFFF, 0x115BFFFF, 0x07D1FFFF,
0x9AA1FFFF, 0x4D87FFFF, 0x442BFFFF, 0xEAA5FFFF, 0xAEDBFFFF, 0xB6A5FFFF, 0xAFE9FFFF, 0xE895FFFF,
0x4E05FFFF, 0xF8BFFFFF, 0xCB5DFFFF, 0x2F85FFFF, 0xF1DFFFFF, 0xD5ADFFFF, 0x438DFFFF, 0x6073FFFF,
@jhaberstro
jhaberstro / ScopeStackDemo.cpp
Created November 5, 2010 22:00
ScopeStack implementation stolen from Andreas Fredriksson's DICE presentation on Scope Stack Allocation.
// Simplified scope stack implementation
#include <new>
#include <cstdio>
#include <cassert>
typedef unsigned char u8;
class LinearAllocator {
public:
@jhaberstro
jhaberstro / gist:7795350
Last active December 30, 2015 07:19
Graphics Architecture Readings
@jhaberstro
jhaberstro / parser_combinator.cpp
Created May 20, 2014 23:00
The beginnings of a C++ parser combinator library
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <tuple>
#include <type_traits>
#include <vector>
template< typename T >
using ParseResult = std::vector<std::tuple<T, std::string>>;
@jhaberstro
jhaberstro / num.swift
Last active June 3, 2016 12:17
Swift typeclass example
protocol Num {
class func zero() -> Self
func succ() -> Self
func add(y: Self) -> Self
func multiply(y: Self) -> Self
}
extension Int32: Num {
static func zero() -> Int32 { return 0 }
func succ() -> Int32 { return self + 1 }
@jhaberstro
jhaberstro / monoid.swift
Last active December 14, 2015 18:39
Swift monoid, num, Sum typeclasses with Int32 instances
protocol Monoid {
class func mzero() -> Self
func mop(y: Self) -> Self
}
func mconcat<M : Monoid>(t: Array<M>) -> M {
return (t.reduce(M.mzero()) { $0.mop($1) })
}
protocol Num {
@jhaberstro
jhaberstro / gist:878f7f2043f922f0d06c
Last active August 29, 2015 14:02
Multiple optional bindings in Swift (i.e. the equivalent of if let (a, b, c) = (optA, optB, optC) { ... })
// The DSL's implementation (i.e. just the applicative interface for Optionals + methods for creating tuples)
operator infix <*> { associativity left }
@infix func <*><A, B>(lhs: (A -> B)?, rhs: A?) -> B? {
switch lhs {
case .None:
return .None
case let .Some(f):
return rhs.map(f)
}
}
@jhaberstro
jhaberstro / signextend.cpp
Created July 18, 2015 04:38
Nifty sign extend
template< unsigned SrcSize, unsigned DstSize >
static inline uint32_t signExtend(uint32_t x)
{
static_assert(SrcSize <= 32, "SrcSize cannot be larger than 32 bits.");
static_assert(SrcSize <= 32, "DstSize cannot be larger than 32 bits.");
static_assert(SrcSize < DstSize, "DstSize must be larger than SrcSize.");
constexpr unsigned src_mask = (1 << SrcSize) - 1;
constexpr unsigned extension = ((1 << (DstSize - SrcSize)) - 1) << SrcSize;
uint32_t result = x & src_mask;
uint32_t is_signed = (x & (1 << (SrcSize - 1))) >> (SrcSize - 1);
@jhaberstro
jhaberstro / prob_data_miners_notes.md
Last active September 29, 2015 00:20
Notes for "Probability for Data Miners"

========================

  • "P(A)" means the probability that the event A will happen/A will be true.
  • The core axioms
    1. 0 <= P(A) <= 1
    2. P(true) = 1
    3. P(false) = 0
    4. P(A or B) = P(A) + P(B) - P(A and B) Note: it seems that this is definition of or is the exclusive or.
  • From the core axioms, we can derive:
@jhaberstro
jhaberstro / linear_algebra_notes.md
Created September 29, 2015 00:23
Notes for MIT's "Linear Algebra 18.06"
Linear Algebra 18.06 notes

=============================

  • Lecture 1: The Geometry of Linear Equations
    • The matrix A (whose columns are u, v, w) times the column vector x (whose components are c, d, e) is the same as the combination cu + dv + ew of the three columns.
  • Lecture 2: Elimination with Matrices
    • Elimination works by creating a triangular matrix, which for a system of 3 equations and three variables gives us a new system of 3 equations that has one equation with only one variable, another with two variables, and the last with three variables. This system can then be easily solved.
  • Lecture 3: Multiplication and Inverse Matrices
    • When multiply matrices, A*B=C, the element in the i-th row and j-th column of the resultant matrix, C, is calculated by taking the dot product of the i-th row of A with the j-th column of B.
  • Another way to view matrix multiplication: in A*B=C, each column of C is a linear combination of the columns of A. e.g.