local X={}local Y=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local w=Random.new()local function A(X)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")local function Y(X)local w=table.create(#X)for X,A in pairs(X)do if type(A)=="\116\097\098\108\101"then w[X]=Y(A)else w[X]=A end end return w end return Y(X)end local function e(X)local Y=table.create(#X)if#X>0 then table.move(X,1,#X,1,Y)else for X,w in pairs(X)do Y[X]=w end end return Y end local function J(X,Y)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")assert(type(Y)=="\116\097\098\108\101","\083\101\099\111\110\100 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")local w=e(X)for X,e in pairs(w)do local P=Y[X]if P==nil then w[X]=nil elseif type(e)~=type(P)then if type(P)=="\116\097\098\108\101"then w[X]=A(P)else w[X]=P end elseif type(e)=="\116\097\098\108\101"then w[X]=J(e,P)end end for X,Y in pairs(Y)do local e=w[X]if e==nil then if type(Y)=="\116\097\098\108\101"then w[X]=A(Y)else w[X]=Y end end end return w end local function P(X,Y)local w=#X X[Y]=X[w]X[w]=nil end local function i(X,Y)local w=table.find(X,Y)if w then P(X,w)return true,w end return false,nil end local function D(X,Y)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")assert(type(Y)=="\102\117\110\099\116\105\111\110","\083\101\099\111\110\100 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110")local w=table.create(#X)for A,e in pairs(X)do w[A]=Y(e,A,X)end return w end local function G(X,Y)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")assert(type(Y)=="\102\117\110\099\116\105\111\110","\083\101\099\111\110\100 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110")local w=table.create(#X)if#X>0 then local A=0 for e,J in ipairs(X)do if Y(J,e,X)then A+=1 w[A]=J end end else for A,e in pairs(X)do if Y(e,A,X)then w[A]=e end end end return w end local function I(X,Y,w)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")assert(type(Y)=="\102\117\110\099\116\105\111\110","\083\101\099\111\110\100 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110")assert(w==nil or type(w)=="\110\117\109\098\101\114","\084\104\105\114\100 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114 \111\114 \110\105\108")local A=w or 0 for w,e in pairs(X)do A=Y(A,e,w,X)end return A end local function x(X,...)local Y=e(X)for X,w in ipairs({...})do for X,w in pairs(w)do Y[X]=w end end return Y end local function Q(X,Y)local w=e(X)for X,Y in ipairs(Y)do table.insert(w,Y)end return w end local function F(X)local Y=#X local w=table.create(Y)for A=1,Y,1 do w[A]=X[(Y-A)+1]end return w end local function H(X,Y)assert(type(X)=="\116\097\098\108\101","\070\105\114\115\116 \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \116\097\098\108\101")local A=e(X)local J=Y or w for X=#X,2,-1 do local Y=J:NextInteger(1,X)A[X],A[Y]=A[Y],A[X]end return A end local function p(X,Y)Y=Y or 1 local w=table.create(#X)local function A(X,e)for X,J in ipairs(X)do if type(J)=="\116\097\098\108\101"and e<Y then A(J,e+1)else table.insert(w,J)end end end A(X,0)return w end local function o(X,Y)return p(D(X,Y))end local function b(X)local Y=table.create(#X)for X in pairs(X)do table.insert(Y,X)end return Y end local function a(X,Y)for w,A in pairs(X)do if Y(A,w,X)then return A,w end end return nil,nil end local function u(X,Y)for w,A in pairs(X)do if not Y(A,w,X)then return false end end return true end local function r(X,Y)for w,A in pairs(X)do if Y(A,w,X)then return true end end return false end local function z(X)return next(X)==nil end local function W(X)return Y:JSONEncode(X)end local function C(X)return Y:JSONDecode(X)end X.Copy=A X.CopyShallow=e X.Sync=J X.FastRemove=P X.FastRemoveFirstValue=i X.Map=D X.Filter=G X.Reduce=I X.Assign=x X.Extend=Q X.Reverse=F X.Shuffle=H X.Flat=p X.FlatMap=o X.Keys=b X.Find=a X.Every=u X.Some=r X.IsEmpty=z X.EncodeJSON=W X.DecodeJSON=C return X
-- Table Util
-- Stephen Leitnick
-- September 13, 2017

--[[

	TableUtil.Copy(tbl: table): table
	TableUtil.CopyShallow(tbl: table): table
	TableUtil.Sync(tbl: table, template: table): void
	TableUtil.FastRemove(tbl: table, index: number): void
	TableUtil.FastRemoveFirstValue(tbl: table, value: any): (boolean, number)
	TableUtil.Map(tbl: table, callback: (value: any) -> any): table
	TableUtil.Filter(tbl: table, callback: (value: any) -> boolean): table
	TableUtil.Reduce(tbl: table, callback: (accum: number, value: number) -> number [, initialValue: number]): number
	TableUtil.Assign(target: table, ...sources: table): table
	TableUtil.Extend(tbl: table, extension: table): table
	TableUtil.Reverse(tbl: table): table
	TableUtil.Shuffle(tbl: table [, rng: Random]): table
	TableUtil.Flat(tbl: table [, maxDepth: number = 1]): table
	TableUtil.FlatMap(tbl: callback: (value: any) -> table): table
	TableUtil.Keys(tbl: table): table
	TableUtil.Find(tbl: table, callback: (value: any) -> boolean): (any, number)
	TableUtil.Every(tbl: table, callback: (value: any) -> boolean): boolean
	TableUtil.Some(tbl: table, callback: (value: any) -> boolean): boolean
	TableUtil.IsEmpty(tbl: table): boolean
	TableUtil.EncodeJSON(tbl: table): string
	TableUtil.DecodeJSON(json: string): table

--]]


local TableUtil = {}

local HttpService = game:GetService("HttpService")
local rng = Random.new()


local function CopyTable(t)
	assert(type(t) == "table", "First argument must be a table")
	local function Copy(tbl)
		local tCopy = table.create(#tbl)
		for k,v in pairs(tbl) do
			if (type(v) == "table") then
				tCopy[k] = Copy(v)
			else
				tCopy[k] = v
			end
		end
		return tCopy
	end
	return Copy(t)
end


local function CopyTableShallow(t)
	local tCopy = table.create(#t)
	if (#t > 0) then
		table.move(t, 1, #t, 1, tCopy)
	else
		for k,v in pairs(t) do tCopy[k] = v end
	end
	return tCopy
end


local function Sync(srcTbl, templateTbl)

	assert(type(srcTbl) == "table", "First argument must be a table")
	assert(type(templateTbl) == "table", "Second argument must be a table")

	local tbl = CopyTableShallow(srcTbl)

	-- If 'tbl' has something 'templateTbl' doesn't, then remove it from 'tbl'
	-- If 'tbl' has something of a different type than 'templateTbl', copy from 'templateTbl'
	-- If 'templateTbl' has something 'tbl' doesn't, then add it to 'tbl'
	for k,v in pairs(tbl) do

		local vTemplate = templateTbl[k]

		-- Remove keys not within template:
		if (vTemplate == nil) then
			tbl[k] = nil

		-- Synchronize data types:
		elseif (type(v) ~= type(vTemplate)) then
			if (type(vTemplate) == "table") then
				tbl[k] = CopyTable(vTemplate)
			else
				tbl[k] = vTemplate
			end

		-- Synchronize sub-tables:
		elseif (type(v) == "table") then
			tbl[k] = Sync(v, vTemplate)
		end

	end

	-- Add any missing keys:
	for k,vTemplate in pairs(templateTbl) do

		local v = tbl[k]

		if (v == nil) then
			if (type(vTemplate) == "table") then
				tbl[k] = CopyTable(vTemplate)
			else
				tbl[k] = vTemplate
			end
		end

	end

	return tbl

end


local function FastRemove(t, i)
	local n = #t
	t[i] = t[n]
	t[n] = nil
end


local function FastRemoveFirstValue(t, v)
	local index = table.find(t, v)
	if (index) then
		FastRemove(t, index)
		return true, index
	end
	return false, nil
end


local function Map(t, f)
	assert(type(t) == "table", "First argument must be a table")
	assert(type(f) == "function", "Second argument must be a function")
	local newT = table.create(#t)
	for k,v in pairs(t) do
		newT[k] = f(v, k, t)
	end
	return newT
end


local function Filter(t, f)
	assert(type(t) == "table", "First argument must be a table")
	assert(type(f) == "function", "Second argument must be a function")
	local newT = table.create(#t)
	if (#t > 0) then
		local n = 0
		for i,v in ipairs(t) do
			if (f(v, i, t)) then
				n += 1
				newT[n] = v
			end
		end
	else
		for k,v in pairs(t) do
			if (f(v, k, t)) then
				newT[k] = v
			end
		end
	end
	return newT
end


local function Reduce(t, f, init)
	assert(type(t) == "table", "First argument must be a table")
	assert(type(f) == "function", "Second argument must be a function")
	assert(init == nil or type(init) == "number", "Third argument must be a number or nil")
	local result = (init or 0)
	for k,v in pairs(t) do
		result = f(result, v, k, t)
	end
	return result
end


local function Assign(target, ...)
	local tbl = CopyTableShallow(target)
	for _,src in ipairs({...}) do
		for k,v in pairs(src) do
			tbl[k] = v
		end
	end
	return tbl
end


local function Extend(target, extension)
	local tbl = CopyTableShallow(target)
	for _,v in ipairs(extension) do
		table.insert(tbl, v)
	end
	return tbl
end


local function Reverse(tbl)
	local n = #tbl
	local tblRev = table.create(n)
	for i = 1,n do
		tblRev[i] = tbl[n - i + 1]
	end
	return tblRev
end


local function Shuffle(tbl, rngOverride)
	assert(type(tbl) == "table", "First argument must be a table")
	local shuffled = CopyTableShallow(tbl)
	local random = (rngOverride or rng)
	for i = #tbl, 2, -1 do
		local j = random:NextInteger(1, i)
		shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
	end
	return shuffled
end


local function Flat(tbl, depth)
	depth = (depth or 1)
	local flatTbl = table.create(#tbl)
	local function Scan(t, d)
		for _,v in ipairs(t) do
			if (type(v) == "table" and d < depth) then
				Scan(v, d + 1)
			else
				table.insert(flatTbl, v)
			end
		end
	end
	Scan(tbl, 0)
	return flatTbl
end


local function FlatMap(tbl, callback)
	return Flat(Map(tbl, callback))
end


local function Keys(tbl)
	local keys = table.create(#tbl)
	for k in pairs(tbl) do
		table.insert(keys, k)
	end
	return keys
end


local function Find(tbl, callback)
	for k,v in pairs(tbl) do
		if (callback(v, k, tbl)) then
			return v, k
		end
	end
	return nil, nil
end


local function Every(tbl, callback)
	for k,v in pairs(tbl) do
		if (not callback(v, k, tbl)) then
			return false
		end
	end
	return true
end


local function Some(tbl, callback)
	for k,v in pairs(tbl) do
		if (callback(v, k, tbl)) then
			return true
		end
	end
	return false
end


local function IsEmpty(tbl)
	return (next(tbl) == nil)
end


local function EncodeJSON(tbl)
	return HttpService:JSONEncode(tbl)
end


local function DecodeJSON(str)
	return HttpService:JSONDecode(str)
end


TableUtil.Copy = CopyTable
TableUtil.CopyShallow = CopyTableShallow
TableUtil.Sync = Sync
TableUtil.FastRemove = FastRemove
TableUtil.FastRemoveFirstValue = FastRemoveFirstValue
TableUtil.Map = Map
TableUtil.Filter = Filter
TableUtil.Reduce = Reduce
TableUtil.Assign = Assign
TableUtil.Extend = Extend
TableUtil.Reverse = Reverse
TableUtil.Shuffle = Shuffle
TableUtil.Flat = Flat
TableUtil.FlatMap = FlatMap
TableUtil.Keys = Keys
TableUtil.Find = Find
TableUtil.Every = Every
TableUtil.Some = Some
TableUtil.IsEmpty = IsEmpty
TableUtil.EncodeJSON = EncodeJSON
TableUtil.DecodeJSON = DecodeJSON


return TableUtil
