Skip to content

Commit

Permalink
Add UniqueIds.GetIdIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Jul 27, 2024
1 parent 1c3210d commit 62b7dfc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Useful Modules/Util/Math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function Math.InverseLerp(a: number, b: number, value: number): number
end

--- @todo
function Math.IsInfNaN(...: number): boolean
function Math.IsReal(...: number): boolean
for _, num in {...} do
if num == math.huge or num == -math.huge or num ~= num then
return true
if num * 0 ~= 0 then
return false
end
end
return false
return true
end

--- @todo
Expand Down
15 changes: 12 additions & 3 deletions Useful Modules/Util/UniqueIds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function UniqueIds.new(bytes: number, b93: boolean?, idsInUse: {string}?): Uniqu
end

return {
GetNewId = function(_self: UniqueIds): string
GetNewId = function(self: UniqueIds): string
if numUnusedIds > 0 then
local id = table.remove(unusedIds, numUnusedIds) :: string
numUnusedIds -= 1
Expand All @@ -71,12 +71,21 @@ function UniqueIds.new(bytes: number, b93: boolean?, idsInUse: {string}?): Uniqu
return false
end;

GetIdIndex = function(_self: UniqueIds, id: string): number
GetIdIndex = function(self: UniqueIds, id: string): number
return if b93 then Base93.B93ToInt(id) else string.unpack("I" .. bytes, id)
end
end;
}
end

--- Returns the index of a unique ID string.
--- @param bytes -- The length of the string.
--- @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
return if b93 then Base93.B93ToInt(id) else string.unpack("I" .. bytes, id)
end

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

return UniqueIds
3 changes: 1 addition & 2 deletions selene.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ unused_variable = "warn"

[config]
shadowing = { ignore_pattern = "^self$|^_" }
unscoped_variables = { ignore_pattern = "^self$" }
unused_variable = { allow_unused_self = true }
unused_variable = { ignore_pattern = "^self$|^_", allow_unused_self = true }

0 comments on commit 62b7dfc

Please sign in to comment.