Created
November 11, 2024 03:34
-
-
Save nightcycle/838540aa8c777fbe681bfe6cac07d35d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--!strict | |
--!native | |
-- Services | |
-- Packages | |
-- Modules | |
-- Types | |
export type Encoding = "i8" | "u8" | "i16" | "u16" | "i32" | "u32" | "f32" | "f64" | |
-- Constants | |
-- Variables | |
-- References | |
-- Private Functions | |
-- Class | |
local Util = {} | |
Util.ENCODING_SIZES_BYTES = { | |
i8 = 1, | |
u8 = 1, | |
i16 = 2, | |
u16 = 2, | |
i32 = 4, | |
u32 = 4, | |
f32 = 4, | |
f64 = 8, | |
} | |
function Util.newOffsetSolver(valueSizeBytes: number, startAtZero: boolean?): (i: number) -> number | |
local function solve(i: number): number | |
return (i * valueSizeBytes) | |
end | |
if startAtZero then | |
return solve | |
else | |
return function(i: number): number | |
return solve(i) | |
end | |
end | |
end | |
function Util.newOffsetSolver2D( | |
xWidth: number, | |
valueSizeBytes: number, | |
startAtZero: boolean? | |
): (x: number, y: number) -> number | |
local function solve(x: number, y: number): number | |
local yStart = y * xWidth * valueSizeBytes | |
return yStart + (x * valueSizeBytes) | |
end | |
if startAtZero then | |
return solve | |
else | |
return function(x: number, y: number): number | |
y = (y - 1) | |
x = (x - 1) | |
return solve(x, y) | |
end | |
end | |
end | |
function Util.newOffsetSolver3D( | |
xWidth: number, | |
yWidth: number, | |
valueSizeBytes: number, | |
startAtZero: boolean? | |
): (x: number, y: number, z: number) -> number | |
local function solve(x: number, y: number, z: number): number | |
local zStart = z * xWidth * yWidth * valueSizeBytes | |
local yStart = y * xWidth * valueSizeBytes | |
return zStart + yStart + (x * valueSizeBytes) | |
end | |
if startAtZero then | |
return solve | |
else | |
return function(x: number, y: number, z: number): number | |
y = (y - 1) | |
x = (x - 1) | |
z = (z - 1) | |
return solve(x, y, z) | |
end | |
end | |
end | |
return Util |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment