Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. var32 cons(var32 x, var32 y){
  2. var32 ptr;
  3. uid id, hashedId;
  4. map newMap, currentMap;
  5. u32 colls;
  6.  
  7. #ifdef __DEBUG__
  8. ++consCount;
  9. colls = 0;
  10. #endif
  11.  
  12. // Before anything, compute the unique id of that node.
  13. id = (((uid)(x.ptr))<<32) | (uid)y.ptr;
  14.  
  15. // Try to find the node. If it already exists, return its ptr.
  16. hashedId = id % MAP_HASH_SIZE;
  17. currentMap = mapByHashedId[hashedId];
  18. if (currentMap) do {
  19. if (mapId[currentMap] == id)
  20. return mapPtr[currentMap];
  21. #ifdef __DEBUG__
  22. ++colls;
  23. #endif
  24. } while ((currentMap = mapNext[currentMap]));
  25.  
  26. #ifdef __DEBUG__
  27. if (colls<16) ++collisionCount[colls];
  28. ++allocCount;
  29. #endif
  30.  
  31. // If does not exist, then alloc a new ptr to it.
  32. ptr = nodeNewPtr[nodeNewPtrIndex--];
  33.  
  34. // Add that new ptr to the map.
  35. newMap = mapNewPtr[mapNewPtrIndex--];
  36. if (mapByHashedId[hashedId])
  37. mapNext[newMap] = mapByHashedId[hashedId];
  38. mapId[newMap] = id;
  39. mapPtr[newMap] = ptr;
  40. mapByHashedId[hashedId] = newMap;
  41.  
  42. // Fill that new ptr.
  43. nodeLeft[GETPTR(ptr)] = x;
  44. nodeRight[GETPTR(ptr)] = y;
  45.  
  46. return ptr;
  47. };
  48.  
  49. currentMap = mapByHashedId[hashedId];
  50. if (currentMap) do {
  51. if (mapId[currentMap] == id)
  52. return mapPtr[currentMap];
  53. #ifdef __DEBUG__
  54. ++colls;
  55. #endif
  56. } while ((currentMap = mapNext[currentMap]));
  57.  
  58. currentMap = mapByHashedId[hashedId];
  59. while (currentMap)
  60. {
  61. if (mapId[currentMap] == id)
  62. return mapPtr[currentMap];
  63.  
  64. #ifdef __DEBUG__
  65. ++colls;
  66. #endif
  67.  
  68. currentMap = mapNext[currentMap]
  69. }
  70.  
  71. x y
  72. --- ---
  73. 0x1 0x1
  74. 0x2 0x1
  75. 0x3 0x1
  76. 0x4 0x1
  77.  
  78. x y id hashedId
  79. --- --- ---------- --------
  80. 0x1 0x1 0x10000001 0x1
  81. 0x2 0x1 0x20000001 0x1
  82. 0x3 0x1 0x30000001 0x1
  83. 0x4 0x1 0x40000001 0x1
  84.  
  85. hashedId = ((x << 7) ^ (y * 31)) % MAP_HASH_SIZE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement