Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created February 20, 2019 03:49
Show Gist options
  • Save pervognsen/aca9ed53fb570ea1639e22a259d1622b to your computer and use it in GitHub Desktop.
Save pervognsen/aca9ed53fb570ea1639e22a259d1622b to your computer and use it in GitHub Desktop.
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