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 b2311a1
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions Useful Modules/Util/UniqueIds.luau
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ local Base93 = require(script.Parent.Base93)
export type UniqueIds = {
GetNewId: (UniqueIds) -> (string);
FreeId: (UniqueIds, string) -> (boolean);
GetIdIndex: (UniqueIds, string) -> (number);
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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,25 +54,21 @@ 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
local index = UniqueIds:GetIndexFromId(bytes, id, b93)
if index < 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)
end;
}
end

Expand All @@ -82,10 +77,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 b2311a1

Please sign in to comment.