mirror of
https://github.com/rxi/shash.git
synced 2024-11-24 08:44:36 +00:00
Made :each()'s order deterministic
As table hashes are generated from their GC pointer, and these pointers are potentially different each time a program is run, using them for keys in a table causes the iteration order to potentially differ between runs, too. This commit changes each_overlapping_entity() to not iterate the set of entities, assuring the order in which :each() passes entities to the callback function is deterministic.
This commit is contained in:
parent
6a6a7dea85
commit
90476217a3
15
shash.lua
15
shash.lua
@ -138,13 +138,14 @@ local function overlaps(e1, e2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function add_each_overlapping_in_cell(self, idx, e, set)
|
local function each_overlapping_in_cell(self, idx, e, set, fn, ...)
|
||||||
local t = self.cells[idx]
|
local t = self.cells[idx]
|
||||||
if not t then
|
if not t then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
for i, v in ipairs(t) do
|
for i, v in ipairs(t) do
|
||||||
if e ~= v and overlaps(e, v) then
|
if e ~= v and overlaps(e, v) and not set[v] then
|
||||||
|
fn(v[5], ...)
|
||||||
set[v] = true
|
set[v] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -152,16 +153,14 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function each_overlapping_entity(self, e, fn, ...)
|
local function each_overlapping_entity(self, e, fn, ...)
|
||||||
-- Init set
|
-- Init set for keeping track of which entities have already been handled
|
||||||
local set = table.remove(self.tablepool) or {}
|
local set = table.remove(self.tablepool) or {}
|
||||||
-- Do overlap checks and add overlapping to set
|
-- Do overlap checks
|
||||||
each_overlapping_cell(self, e, add_each_overlapping_in_cell, e, set)
|
each_overlapping_cell(self, e, each_overlapping_in_cell, e, set, fn, ...)
|
||||||
-- Do callback for each entity in set and clear set
|
-- Clear set and return to pool
|
||||||
for v in pairs(set) do
|
for v in pairs(set) do
|
||||||
fn(v[5], ...)
|
|
||||||
set[v] = nil
|
set[v] = nil
|
||||||
end
|
end
|
||||||
-- Return set to pool
|
|
||||||
table.insert(self.tablepool, set)
|
table.insert(self.tablepool, set)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user