It looks like you're new here. If you want to get involved, click one of these buttons!
-- -- UID.lua - A simple UID (Unique Identifier) generator, by Antix Software 2015 -- UID = Core.class() require "bit" function UID:init() self.bitPool = {1} -- -- FIRST UID WILL ALWSYS BE 1 end function UID:shutDown() -- EMPTY AND DESTROY BITPOOL for k in pairs (self.bitPool) do self.bitPool[k] = nil end self.bitPool = nil end function UID:get() -- GET NEXT UID for n = 1, #self.bitPool do for i = 0, 31 do if bit.band(self.bitPool[n], bit.rol(1, i)) == 0 then -- FIND FIRST FREE BIT self.bitPool[n] = self.bitPool[n] + bit.rol(1, i) -- SET BIT ALLOCATED return ((n - 1) * 32) + i -- RETURN UID end end end table.insert(self.bitPool, 1) -- BITPOOL IS FULL SO MAKE IT BIGGER return ((#self.bitPool - 1) * 32) -- RETURN UID end function UID:free(uid) -- RELEASE UID FOR RE-USE local b = math.fmod(uid, 32) local i = math.floor(uid / 32) + 1 self.bitPool[i] = bit.band(self.bitPool[i], bit.rol(0xfffffffe, b)) -- CLEAR BIT end |
Comments
https://deluxepixel.com
allocated={}
.
.
.
if allocated[no] then ....
else
end
https://deluxepixel.com
https://deluxepixel.com
Likes: SinisterSoft
Likes: antix
https://deluxepixel.com
Likes: SinisterSoft