Skip to content

Instantly share code, notes, and snippets.

@pervognsen pervognsen/namemap2.ion
Last active Feb 20, 2019

Embed
What would you like to do?
struct NameNode {
len: uint32;
buf: char[1];
}
struct NameMap {
nodes: {uint64, NameNode*}[];
collisions: NameNode*[];
}
func namemap_getn(names: NameMap*, buf: char const*, len: usize): char const* {
#assert(len <= UINT32_MAX);
h := hash(buf, len);
i := hget(names.nodes, h);
if (i != alen(names.nodes)) {
if (node := names.nodes[i][1]; node.len == len && memcmp(node.buf, buf, len) == 0) {
return node.buf;
}
for (k := 0; k < alen(names.collisions); k++) {
if (node := names.collisions[k]; node.len == len && memcmp(node.buf, buf, len) == 0) {
return node.buf;
}
}
}
node: NameNode* = malloc(offsetof(NameNode, buf) + len + 1);
node.len = len;
memcpy(node.buf, buf, len);
node.buf[len] = 0;
if (i != alen(names.nodes)) {
apush(names.collisions, node);
} else {
hput(names.nodes, h, node);
}
return node.buf;
}
func namemap_get(names: NameMap*, str: char const*): char const* {
return namemap_getn(names, str, strlen(str));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.