Skip to content

Instantly share code, notes, and snippets.

@exelotl
exelotl / ialloc.h
Last active December 22, 2015 11:39
A macro that allocates and initialises memory simultaneously.
// allocate memory for a type, and initialise it at the same time
#define ialloc(t, ...) ialloc_impl(sizeof(t), &(t){ __VA_ARGS__ })
// helper function
inline static void* ialloc_impl(int size, void* src) {
void* dest = malloc(size);
memcpy(dest, src, size);
return dest;
}
@exelotl
exelotl / list.h
Created December 30, 2013 00:35
Simple self-resizing list of void pointers
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct {
int capacity;
int size;
void** data;
} list_t;
@exelotl
exelotl / vec2_hash.ooc
Created April 23, 2015 16:10
Defining a custom hash function for structures
import structs/HashMap
Vec2: cover {
x, y:Int
}
vec2equals: func <T> (_a, _b:T) -> Bool {
a := _a as Vec2
b := _b as Vec2
a x == b x && a y == b y
function remove(t,n)
for i=n, count(t) do
t[i] = t[i+1]
end
end
tbl = {10,20,30,40,50}
remove(tbl, 2)
foreach(tbl, print)
import structs/ArrayList
import threading/Thread
counter := 0
mutex := Mutex new()
threads := ArrayList<Thread> new()
main: func {
for (i in 0..10) {
@exelotl
exelotl / pootis.lua
Created August 27, 2015 19:32
is there a proper name for this?
function pootis(f, ...)
local t = {}
for i,v in ipairs({...}) do
t[i] = f(v)
end
return unpack(t)
end
-- example usage
-- > pootis(math.floor, 1.1, 2.2, 3.3, 4.4)
@exelotl
exelotl / fix_cel_tiles.lua
Created September 7, 2015 16:43
Simple script to fix the GBA tile data output by Pro Motion 6.5
-- example usage:
-- lua fix_cel_tiles.lua old_tiles.cel new_tiles.cel
local inpath, outpath = ...
local infile = assert(io.open(inpath, "rb"))
local outfile = assert(io.open(outpath,"wb"))
local bytes_per_tile = 8 * 8
@exelotl
exelotl / stringplus.nim
Created July 17, 2018 21:35
define a `+` operator for string concatenation
proc `+`*(a:string, b:string):string = a & b
proc `+`*[T](a:string, b:T):string = a & $b
proc `+`*[T](a:T, b:string):string = $a & b
@exelotl
exelotl / dispatch.nim
Last active September 13, 2018 22:00
nim dynamic dispatch test
import strformat
type Entity = ref object of RootObj
tid: int # tid uniquely identifies which subclass we are (e.g. Foo or Bar)
type Foo = ref object of Entity
x, y: float
name: string
type Bar = ref object of Entity
@exelotl
exelotl / ambiguous_fn_ptr.nim
Created September 11, 2018 23:40
using type conversion syntax to choose the desired version of an overloaded function
proc foo(x:int) = echo "int ", x
proc foo(x:string) = echo "string ", x
var p1:pointer = (proc(x:int){.nimcall.})(foo) # p1 gets the int version of foo
var p2:pointer = (proc(x:string){.nimcall.})(foo) # p2 gets the string version of foo
var f1 = cast[proc(x:int){.nimcall.}](p1)
var f2 = cast[proc(x:string){.nimcall.}](p2)
f1(100)