Skip to content

Instantly share code, notes, and snippets.

@Vurv78
Created July 13, 2021 00:27
Show Gist options
  • Save Vurv78/3bd7669e7005d373340927014a181d4b to your computer and use it in GitHub Desktop.
Save Vurv78/3bd7669e7005d373340927014a181d4b to your computer and use it in GitHub Desktop.
Small but Inefficient ID alloc
local MaxConcurrent = 512 -- Max concurrent active objects
local TopID = 0 -- Current top-most id.
local OccupiedCount = 0
local Occupied = {}
local function allocID(n)
if OccupiedCount >= MaxConcurrent then return end
local new = (n or TopID) % MaxConcurrent + 1
if Occupied[new] then
return allocID(new)
else
Occupied[new] = true
OccupiedCount = OccupiedCount + 1
TopID = new
return new
end
end
local function dropID(id)
if Occupied[id] then
Occupied[id] = nil
OccupiedCount = OccupiedCount - 1
return true
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment