Skip to content

Commit

Permalink
Update UniqueIds
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Aug 28, 2024
1 parent 1456fed commit 5a4e6d7
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions Useful Modules/Util/UniqueIds.luau
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ local Base93 = require(script.Parent.Base93)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

export type UniqueIds = {
GetNewId: (UniqueIds) -> (string);
FreeId: (UniqueIds, string) -> (boolean);
GetIdIndex: (UniqueIds, string) -> (number);
GetNewId: (UniqueIds) -> string;
FreeId: (UniqueIds, string) -> ();
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -36,11 +35,11 @@ function UniqueIds.new(bytes: number, b93: boolean?, idsInUse: {string}?): Uniqu

if idsInUse then
for _, id in idsInUse do
numUniqueIds = math.max(numUniqueIds, (if b93 then Base93.B93ToInt(id) else string.unpack("I" .. bytes, id)) + 1)
numUniqueIds = math.max(numUniqueIds, UniqueIds:GetIndexFromId(bytes, id, b93) + 1)
end

for index = 1, numUniqueIds do
local id = if b93 then Base93.IntToB93(index - 1, bytes) else string.pack("I" .. bytes, index - 1)
local id = UniqueIds:GetIdFromIndex(bytes, index - 1, b93)
if not table.find(idsInUse, id) then
table.insert(unusedIds, id)
numUnusedIds += 1
Expand All @@ -55,24 +54,17 @@ function UniqueIds.new(bytes: number, b93: boolean?, idsInUse: {string}?): Uniqu
numUnusedIds -= 1
return id
else
local id = if b93 then Base93.IntToB93(numUniqueIds, bytes) else string.pack("I" .. bytes, numUniqueIds)
local id = UniqueIds:GetIdFromIndex(bytes, numUniqueIds, b93)
numUniqueIds += 1
return id
end
end;

FreeId = function(self: UniqueIds, id: string): boolean
local index = self:GetIdIndex(id)
if index + 1 <= numUniqueIds then
table.insert(unusedIds, id)
numUnusedIds += 1
return true
end
return false
end;

GetIdIndex = function(self: UniqueIds, id: string): number
return if b93 then Base93.B93ToInt(id) else string.unpack("I" .. bytes, id)
FreeId = function(self: UniqueIds, id: string): ()
local index = UniqueIds:GetIndexFromId(bytes, id, b93)
assert(index < numUniqueIds and not table.find(unusedIds, id), "ID not in use")
table.insert(unusedIds, id)
numUnusedIds += 1
end;
}
end
Expand All @@ -82,10 +74,19 @@ end
--- @param id -- The unique ID string.
--- @param b93 -- Whether or not to use the Base93 format.
--- @return number -- The index of the unique ID string.
function UniqueIds:GetIdIndex(bytes: number, id: string, b93: boolean?): number
function UniqueIds:GetIndexFromId(bytes: number, id: string, b93: boolean?): number
return if b93 then Base93.B93ToInt(id) else string.unpack("I" .. bytes, id)
end

--- Returns the unique ID string of an index.
--- @param bytes -- The length of the string.
--- @param index -- The index of the unique ID string.
--- @param b93 -- Whether or not to use the Base93 format.
--- @return string -- The unique ID string.
function UniqueIds:GetIdFromIndex(bytes: number, index: number, b93: boolean?): string
return if b93 then Base93.IntToB93(index, bytes) else string.pack("I" .. bytes, index)
end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return UniqueIds

0 comments on commit 5a4e6d7

Please sign in to comment.