Last active
April 21, 2022 14:19
-
-
Save pervognsen/c9d18e8012ce497e7a92d3f86c112337 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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