Skip to content

Instantly share code, notes, and snippets.

@saolsen
saolsen / 2016-04-13.c
Last active June 1, 2016 20:31
circular chat buffer
#if 0
echo Compiling...
cc -Wall -g -std=c99 $0 -o 2016-04-13 && ./2016-04-13
exit
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
@saolsen
saolsen / workq.c
Created December 2, 2015 00:35
another lock free work queue
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <libkern/OSAtomic.h>
#include <unistd.h>
typedef void(*WorkFunction)(void* work_data);
@saolsen
saolsen / multicore.c
Last active June 1, 2016 20:33
sort and print a giant list
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#define TEST_CASES 1000000
#define TEST_RUNS 30
// Channels for passing data between threads. We pack a whole cache line of ints before
#include <stdlib.h>
int main(int nargs, char * args[])
{
void* vp = malloc(sizeof(char) * 5);
const char* ip = (char*)(vp);
free(ip);
}
@saolsen
saolsen / number-to-binary.rkt
Created August 26, 2014 16:39
number to binary
#lang racket
(define (get-spaces-inner n spaces)
(define current-space (car spaces))
(if (> current-space n)
(cdr spaces)
(get-spaces-inner n (cons (* current-space 2) spaces))))
(define (get-spaces n)
(get-spaces-inner n '(1)))
@saolsen
saolsen / goup.clj
Last active January 2, 2016 00:29
Embedding Clojure
(ns goup-engine.core
(:import (com.jme3.app SimpleApplication)
(com.jme3.material Material)
(com.jme3.math ColorRGBA)
(com.jme3.math Vector3f FastMath Quaternion)
(com.jme3.renderer RenderManager)
(com.jme3.scene Geometry Node)
(com.jme3.scene.shape Box Quad)
(com.jme3.input KeyInput)
(com.jme3.input.controls KeyTrigger ActionListener)
@saolsen
saolsen / particles-simulate.cljs
Last active December 30, 2015 07:59
particle simulation
(ns saolsen.particles-simulate
(:require [saolsen.draw-2d :as draw]))
;; Simple simulated neutonian physics.
(defn integrate-particle
"Moves the particle a distance based on it's velocity
P = P + V*dt
Simple collisions with walls reverse velocity.
Returns a new particle.
"
@saolsen
saolsen / draw-2d.cljs
Last active June 22, 2021 06:45
2d canvas drawing in clojurescript
(ns saolsen.draw-2d)
;; Draw stuff (and never care about ie ever)
;; There's obviously a ton this lib doesn't do, just adding what
;; I need when I need it.
(def request-animation-frame
(or js/requestAnimationFrame
js/webkitRequestAnimationFrame))
(defn get-canvas-context-from-id
@saolsen
saolsen / profiling.clj
Last active December 15, 2015 23:29
ghetto profiler
(ns game.macros.profiling)
;; Must require this and also
;; (:use [game.profiling :only [start-time! stop-time!]])
(defmacro profile
"Wraps the body in the game.profiling calls"
[function-name & body]
`(let [profile# (start-time! ~function-name)
result# ~@body]
@saolsen
saolsen / gist:5305308
Last active December 15, 2015 18:39
reduce!
(defn reducer
;; Removes 5 and the item after it from a sequence.
[{:keys [flag seq]} item]
(cond
flag {:flag false :seq seq}
(= item 5) {:flag true :seq seq}
:else {:flag false :seq (conj seq item)}))
(:seq (reduce reducer {:flag false :seq []} [1 2 3 4 5 6 7 8 9 10]))