--sort by values

newTable.vsort = function(tbl, sort)
	sort = sort or function(x, y)
		return x > y
	end
	
	return newTable.xsort(tbl, function(x, y)
		return sort(tbl[x], tbl[y])
	end)
end

newTable.choice = function(tbl, resultCount)
	resultCount = resultCount or 1
	
	local results = {}
	
	--clone the table
	local clone = {}
	
	for i,v in pairs(tbl) do
		clone[i] = v
	end
	
	local keys = newTable.keys(clone)
	
	for i=1,math.min(#tbl, math.max(resultCount, 1)) do
		table.insert(results, tbl[table.remove(keys, math.random(#keys))])
	end
	
	return unpack(results)
end

newTable.one = function(tbl)
	return tbl[math.random(#tbl)]
end

newTable.shallow = function(tbl)
	local clone = {}
	
	for key,val in pairs(tbl) do
		clone[key] = val
	end
	
	return clone
end

newTable.deep = function(tbl)
	local clone = {}
	
	for key,val in pairs(tbl) do
		if typeof(val) == "table" then
			clone[key] = newTable.deep(val)
		else
			clone[key] = val
		end
	end
	
	return clone
end

newTable.cloneKeys = function(tbl)
	local ret = {}
	
	for key,_ in pairs(tbl) do
		ret[key] = true
	end
	
	return ret
end

newTable.dump = function(tbl, prefix)
	prefix = prefix or ""
	
	for key,val in pairs(tbl) do
		print(("%s%s = %s"):format(prefix, tostring(key), tostring(val)))
		
		if typeof(val) == "table" then
			newTable.dump(val, ("%s.%s"):format(prefix, key))
		end
	end
end

newTable.merge = function(tbl, ...)
	local ret = {}
	
	for _,tbl in pairs({tbl, ...}) do
		for _,value in pairs(tbl) do
			table.insert(ret, value)
		end
	end
	
	return ret
end

return setmetatable(newTable, {
	__index = table
})
