-
Notifications
You must be signed in to change notification settings - Fork 3
/
Array.lua
52 lines (40 loc) · 1.1 KB
/
Array.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- Utility Array functions
-- This Library is not for a bunch of for-loop wrapper functions.
-- Either write your own for-loops or learn python instead
-- @author Validark
local Resources = require(game:GetService("ReplicatedStorage"):WaitForChild("Resources"))
local Table = Resources:LoadLibrary("Table")
local type = type
local insert = table.insert
local remove = table.remove
local Array = {}
function Array.Flatten(a1)
-- Takes in an array, which may have arrays inside of it
-- Unpacks all arrays in their proper place into a1
-- e.g. a1 = {{1, 2}, 3, 4, {5, {6, {{7, 8}, 9}, 10}, 11}, 12}
-- becomes: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
local i = 1
local numt = #a1
while i <= numt do
if type(a1[i]) == "table" then
local a2 = remove(a1, i)
local numv = #a2
for j = 1, numv do
insert(a1, i + j - 1, a2[j])
end
numt = numt + numv - 1
else
i = i + 1
end
end
return a1
end
function Array.Contains(a1, v)
-- Returns the index at which v exists within array a1 if applicable
for i = 1, #a1 do
if a1[i] == v then
return i
end
end
end
return Table.Lock(Array)