Created
July 13, 2021 00:27
-
-
Save Vurv78/3bd7669e7005d373340927014a181d4b to your computer and use it in GitHub Desktop.
Small but Inefficient ID alloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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