-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspatial_hash.lua
More file actions
43 lines (36 loc) · 794 Bytes
/
spatial_hash.lua
File metadata and controls
43 lines (36 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
hash = {}
hash.__index = hash
function hash:new(nRows, nCols)
local h = {}
h.loc = {}
h.nRows = nRows
h.nCols = nCols
local total = nRows * nCols
for i = 1, total do
table.insert(h.loc, {})
end
return setmetatable(h, hash)
end
function hash:calcPos(row, col)
return ((row - 1) * self.nRows) + col
end
function hash:add(t, row, col)
local pos = self:calcPos(row, col)
local index = #self.loc[pos] + 1
self.loc[pos][index] = t
return index
end
function hash:rem(row, col, pos)
local p = self:calcPos(row, col)
local t = self.loc[p][pos]
self.loc[p][pos] = nil
return t
end
function hash:update(oR, oC, pos, nR, nC)
return self:add(self:rem(oR, oC, pos), nR, nC)
end
function hash:show(row, col)
local pos = self:calcPos(row, col)
return self.loc[pos]
end
return hash