Skip to content

Instantly share code, notes, and snippets.

View cslarsen's full-sized avatar

Christian Stigen Larsen cslarsen

View GitHub Profile
@cslarsen
cslarsen / v
Created March 19, 2015 08:00
Usage: v <some file>:lineno or v <somefile> +lineno or <somefile> lineno (i think)... searches for files and opens them in vim
#!/usr/bin/env python
"""
Starts vim with several possible input formats.
Examples:
v foo/bar/baz.c # full correct path
v baz.c # finds baz.c and edits the first
v baz.c:123 # same but start at line 123
v baz.c +123 # same as above
@cslarsen
cslarsen / memoize.py
Created February 20, 2012 14:26
Memoization decorator in Python
"""
Implementation of memoization decorator in Python.
Written by Christian Stigen Larsen
http://csl.sublevel3.org
Put in the public domain by the author, 2012
This is an example of how to use decorators to implement different kind of
behaviour in Python.
@cslarsen
cslarsen / pids.py
Created June 8, 2016 08:16
Prints PIDs for given program basename (or wildcard if not found)
#!/usr/bin/env python3
from collections import defaultdict
import os
import sys
def read_cmdline(file):
try:
with open(file, "rb") as f:
cmd = str(f.read(), encoding="utf-8")
@cslarsen
cslarsen / pwait.sh
Created June 8, 2016 07:42
BASH program that waits until given PIDs have terminated. Rename to pwait and chmod +x.
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: pwait <PID>"
echo "Waits until PID has terminated, then continues."
echo "Example: pwait 123 && echo DONE"
exit 1
fi
for pid in "$@"; do
@cslarsen
cslarsen / Makefile
Created January 29, 2014 11:29
How to embed binary objects in your executable.
TARGETS = a.o foo.o a
run: a
./a
all: $(TARGETS)
foo.o: foo.txt
ld -r -b binary -o $@ $<
@cslarsen
cslarsen / lab0.cpp
Created November 19, 2012 20:26
Example of a race condition with and wihout locking (pthread or x86 TSL)
/*
* =================================================
* DAT320 Operating Systems, University of Stavanger
*
* LAB 0: PROTECTION AND THREADS
* Written by Christian Stigen Larsen, 2012-08-24
* =================================================
*
* ABOUT
* -----
@cslarsen
cslarsen / celsius-fahrenheit.scm
Created September 8, 2012 14:35
R7RS version of Celsius-Fahrenheit table
#|
| Celsius-Fahrenheit temperature table
|
| Taken from
| http://programmingpraxis.com/2012/09/07/the-first-two-programs/2/
|
| Ported to current R7RS scheme by Christian Stigen Larsen
| Public Domain, 2012
|
| Works in Mickey Scheme
@cslarsen
cslarsen / mul.cpp
Created June 8, 2012 15:23
Shows how gcc cancels bad "optimizations"
/*
* Simple program I wrote after reading Felix Von Leitner's notes
* on optimization at http://www.fefe.de/source-code-optimization.pdf
*
* In the 90s, it used to be faster to do (x<<8)+(x<<6) than x*320,
* but today with the Intel Core i7, it's actually faster to do
* x*320.
*
* And the compiler knows it!
*
@cslarsen
cslarsen / reverse_string.cpp
Created February 16, 2012 19:04
Reverse string in "canonical" C++
/*
* Reverse string in "canonical" C++
*
* Why did I make this a gist? Because I've seen so
* many cumbersome ways of doing this in C++, including
* using <algorithm> and std::reverse, using loops and
* so on. Yeah, those work too, and loops are fine for
* C, but in C++, you should simply just use the iterators
* and standard constructors that std::string gives you.
*
@cslarsen
cslarsen / prime_sieve_heap.hpp
Created January 10, 2012 18:52
Sieve of Eratosthenes using bitsets on the heap
/*
* A simple sieve of Eratosthenes using bitsets, written by Christian Stigen Larsen
* You are free to use this code for anything (i.e., public domain).
*
* This sieve allocates the bitset on the heap, so you can calculate a lot
* of prime numbers.
*/
#ifndef INC_PRIMES_H
#define INC_PRIMES_H