Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. struct NameNode {
  2. len: uint32;
  3. buf: char[1];
  4. }
  5.  
  6. struct NameMap {
  7. nodes: {uint64, NameNode*}[];
  8. collisions: NameNode*[];
  9. }
  10.  
  11. func namemap_getn(names: NameMap*, buf: char const*, len: usize): char const* {
  12. #assert(len <= UINT32_MAX);
  13. h := hash(buf, len);
  14. i := hget(names.nodes, h);
  15. if (i != alen(names.nodes)) {
  16. if (node := names.nodes[i][1]; node.len == len && memcmp(node.buf, buf, len) == 0) {
  17. return node.buf;
  18. }
  19. for (k := 0; k < alen(names.collisions); k++) {
  20. if (node := names.collisions[k]; node.len == len && memcmp(node.buf, buf, len) == 0) {
  21. return node.buf;
  22. }
  23. }
  24. }
  25. node: NameNode* = malloc(offsetof(NameNode, buf) + len + 1);
  26. node.len = len;
  27. memcpy(node.buf, buf, len);
  28. node.buf[len] = 0;
  29. if (i != alen(names.nodes)) {
  30. apush(names.collisions, node);
  31. } else {
  32. hput(names.nodes, h, node);
  33. }
  34. return node.buf;
  35. }
  36.  
  37. func namemap_get(names: NameMap*, str: char const*): char const* {
  38. return namemap_getn(names, str, strlen(str));
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement