Skip to content

Instantly share code, notes, and snippets.

@mcejp
mcejp / ring_buffer.c
Last active May 7, 2025 13:55
Ring buffer with Array + two indices mod 2N
// Background: https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
// Variant with indices modulo 2*capacity as suggested by dizzy57 and Aristotle Pagaltzis
size_t read;
size_t write;
mask(val) { return val % array.capacity; }
inc(index) { return wrap(index + 1); }
push(val) { assert(!full()); array[mask(write)] = val; write = inc(write); }
shift() { assert(!empty()); ret = array[mask(read)]; read = inc(read); return ret; }
@lukhnos
lukhnos / build.gradle
Created July 10, 2015 06:03
A typical Java project gradle setup that combines META-INF/services in the "super JAR"
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
// ...
}