Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active April 21, 2022 14:19
Show Gist options
  • Save pervognsen/c9d18e8012ce497e7a92d3f86c112337 to your computer and use it in GitHub Desktop.
Save pervognsen/c9d18e8012ce497e7a92d3f86c112337 to your computer and use it in GitHub Desktop.
typedef T = int;
var segs: T**;
var seg: T*;
var segcap = 1;
var segidx = 1;
func grow() {
seg = 0;
asetcap(seg, segcap);
apush(segs, seg);
segcap *= 2;
}
func add(x: T): usize {
if (alen(seg) == acap(seg)) {
grow();
}
apush(seg, x);
return segidx++;
}
func get(i: usize): T* {
e := log2(i);
b := pow2(e);
return &segs[e][i-b];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment