-- Purely for table returning functions.

local function RemoveTemplateItemEntry(intplt,item,...)
	local args = {...}
	for i=1, TableLength(args) do
		if item == LookupItemInTemplate(intplt,args[i]) then
			intplt = RemoveTemplateEntry(intplt,args[i])
		end
	end
	return intplt
end

local function SubtractTemplates(intplt,intplt2)
	for i=1, TableLength(intplt2) do
		intplt = RemoveTemplateItemEntry(intplt,TableUnpack(intplt2[i]))
	end
	return intplt,intplt2
end

local function RetrieveTemplatesInTemplate(intplt)
	local outtbl = {}
	for entry=1, TableLength(intplt) do
		RecursiveTableAdd(outtbl,Select(IsTemplateToBool(intplt[entry][1]),intplt[entry][1]))
	end
	return outtbl
end

local function DetermineTemplateComponents(intplt,intplttbl)
	local outtbl = {}
	for template=1, TableLength(intplttbl) do
		RecursiveTableAdd(outtbl,Select(TemplateContainsToBool(intplt,intplttbl[template]),intplttbl[template]))
	end
	return outtbl
end

local function IndexTemplateIntoDDB(intplt,id,indbtplt)
	-- Create a local database space containing only what we see here.
	local changestplt = {}
	-- Lookup the template in the matrix to see if it has already been established.
	if AndToBool(AndToBool(NilToBool(LookupItemInTemplate(indbtplt,intplt)),NilToBool(LookupItemInTemplate(indbtplt,id))),NotToBool(RecursiveDetermineTemplateEqualityToBool(intplt,RetrieveTemplatesInTemplate(indbtplt)))) then
		changestplt = {{intplt,id},{id,intplt}}
	end
	-- The output returned will be the changed database template, the templated changes that would be made, and the original database template.
	return ConcatenateTemplates(ConcatenateTemplates({},indbtplt),changestplt),changestplt,indbtplt
end