Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active February 20, 2019 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/e72475ddda1757997479fb9d3e198605 to your computer and use it in GitHub Desktop.
Save pervognsen/e72475ddda1757997479fb9d3e198605 to your computer and use it in GitHub Desktop.
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