Skip to content

Instantly share code, notes, and snippets.

@pervognsen pervognsen/namemap.c
Created Feb 20, 2019

Embed
What would you like to do?
struct std_NameNode {
std_NameNode (*next);
uint32_t len;
char (buf[1]);
};
struct tuple4 {
ullong _0;
std_NameNode (*_1);
};
struct std_NameMap {
tuple4 (*nodes);
};
char (*std_namemap_getn(std_NameMap (*names), char (*buf), size_t len)) {
ullong h = std_hash(buf, len);
ullong i = hget(ullong, (names->nodes), (h));
std_NameNode (*node) = {0};
if ((i) != (std_alen(names->nodes))) {
node = names->nodes[i]._1;
for (std_NameNode (*it) = node; it; it = it->next) {
if (((len) == (it->len)) && ((memcmp(buf, it->buf, len)) == (0))) {
return it->buf;
}
}
}
std_NameNode (*new_node) = malloc(((offsetof(std_NameNode, buf)) + (len)) + (1));
new_node->next = node;
new_node->len = len;
memcpy(new_node->buf, buf, len);
new_node->buf[len] = 0;
hput(tuple4, _0, (names->nodes), (h), (new_node));
return new_node->buf;
}
char (*std_namemap_get(std_NameMap (*names), char (*str))) {
return std_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.