diff --git a/Useful Modules/Util/UniqueIds.luau b/Useful Modules/Util/UniqueIds.luau index f728465..17e2fa2 100644 --- a/Useful Modules/Util/UniqueIds.luau +++ b/Useful Modules/Util/UniqueIds.luau @@ -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) -> (); } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -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 @@ -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 @@ -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