Professional Documents
Culture Documents
11 Hashing
11 Hashing
• If we choose ℎ = 2𝑟
we only need to check the containing and the surrounding cells
• 9 in 2 dimensions, 27 in 3 dimensions
Storing the Grid
xi = Math.floor(px / spacing)
spacing
yi = Math.floor(py / spacing)
5
1 3
numY
i = xi * numY + yi
4 2 i = (xi * numY + yi) * numZ + zi
numX
numX * numY
5 2 3
1 4
Dense Grid Representation
5
1 3
i = xi * numY + yi
4 2
0 0 0 0 0 0 2 2 2 2 2 2 4 4 5 5 5 5 5 5 5
numX * numY + 1
5 1 2 4 3
numParticles
Handle Unbounded Grid with Hashing
5
1 3
i = hash(xi,yi) % tableSize
4 2
0 0 0 0 0 0 0 0 3 3 3 3 3 3 5 5 5 5 5 5
tableSize
3 4 2 1 5
hash collision OK
Hashing Remarks
• The hash function must return a value that distributes the cells evenly
• Partial sums
0 0 0 0 0 2 2 2 2 2 2 4 4 5 5 5 5 5 5 5 5
• Fill in
1
0 0 0 0 0 1 2 2 2 2 2 4 4 5 5 5 5 5 5 5 5 1
2
0 0 0 0 0 1 2 2 2 2 2 3 4 5 5 5 5 5 5 5 5 1 2
3
0 0 0 0 0 1 2 2 2 2 2 3 4 4 5 5 5 5 5 5 5 1 23
4
0 0 0 0 0 1 2 2 2 2 2 2 4 4 5 5 5 5 5 5 5 1423
5
0 0 0 0 0 0 2 2 2 2 2 2 4 4 5 5 5 5 5 5 5 51423
Let’s implement it…