Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
struct NameNode {
next: NameNode*;
len: uint32;
buf: char[1];
}
struct NameMap {
nodes: {uint64, NameNode*}[];
}
func namemap_getn(names: NameMap*, buf: char const*, len: usize): char const* {
h := hash(buf, len);
i := hget(names.nodes, h);
node: NameNode*;
if (i != alen(names.nodes)) {
node = names.nodes[i][1];
for (it := node; it; it = it.next) {
if (len == it.len && memcmp(buf, it.buf, len) == 0) {
return it.buf;
}
}
}
new_node: NameNode* = malloc(offsetof(NameNode, buf) + len + 1);
new_node.next = node;
new_node.len = len;
memcpy(new_node.buf, buf, len);
new_node.buf[len] = 0;
hput(names.nodes, h, new_node);
return new_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.