Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var32 cons(var32 x, var32 y){
- var32 ptr;
- uid id, hashedId;
- map newMap, currentMap;
- u32 colls;
- #ifdef __DEBUG__
- ++consCount;
- colls = 0;
- #endif
- // Before anything, compute the unique id of that node.
- id = (((uid)(x.ptr))<<32) | (uid)y.ptr;
- // Try to find the node. If it already exists, return its ptr.
- hashedId = id % MAP_HASH_SIZE;
- currentMap = mapByHashedId[hashedId];
- if (currentMap) do {
- if (mapId[currentMap] == id)
- return mapPtr[currentMap];
- #ifdef __DEBUG__
- ++colls;
- #endif
- } while ((currentMap = mapNext[currentMap]));
- #ifdef __DEBUG__
- if (colls<16) ++collisionCount[colls];
- ++allocCount;
- #endif
- // If does not exist, then alloc a new ptr to it.
- ptr = nodeNewPtr[nodeNewPtrIndex--];
- // Add that new ptr to the map.
- newMap = mapNewPtr[mapNewPtrIndex--];
- if (mapByHashedId[hashedId])
- mapNext[newMap] = mapByHashedId[hashedId];
- mapId[newMap] = id;
- mapPtr[newMap] = ptr;
- mapByHashedId[hashedId] = newMap;
- // Fill that new ptr.
- nodeLeft[GETPTR(ptr)] = x;
- nodeRight[GETPTR(ptr)] = y;
- return ptr;
- };
- currentMap = mapByHashedId[hashedId];
- if (currentMap) do {
- if (mapId[currentMap] == id)
- return mapPtr[currentMap];
- #ifdef __DEBUG__
- ++colls;
- #endif
- } while ((currentMap = mapNext[currentMap]));
- currentMap = mapByHashedId[hashedId];
- while (currentMap)
- {
- if (mapId[currentMap] == id)
- return mapPtr[currentMap];
- #ifdef __DEBUG__
- ++colls;
- #endif
- currentMap = mapNext[currentMap]
- }
- x y
- --- ---
- 0x1 0x1
- 0x2 0x1
- 0x3 0x1
- 0x4 0x1
- x y id hashedId
- --- --- ---------- --------
- 0x1 0x1 0x10000001 0x1
- 0x2 0x1 0x20000001 0x1
- 0x3 0x1 0x30000001 0x1
- 0x4 0x1 0x40000001 0x1
- hashedId = ((x << 7) ^ (y * 31)) % MAP_HASH_SIZE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement