Module:User:Erutuon/04

local export = {}

local dump = require("Module:debug").highlight_dump

local languageData = require("Module:User:Erutuon/04/data")

local function dumbSearch(t, item, func) local useFunc = type(func) == "function" for i, val in ipairs(t) do		if useFunc and func(val) == item or val == item then return i		end end return nil end

--]	Does a binary search in a table.	The table of functions can contain three functions:	--	getCompVal, which is applied to the table entry to get the value to be		compared against the value that is being searched for;	--	comp, which returns true when the value being searched for is less than		(that is, will be found at a lower index in the table than) the table		value currently being looked at;	--	equals, which returns true when the value being searched for is equal to		the current table value being looked at (and hence should be returned by		the search function). function export.binarySearch(t, value, functions) if functions == nil then functions = {} end -- Initialize functions. -- Function that returns value used to compare. local getCompVal = functions.getCompVal or function(x) return x end -- Function that compares the two values. local comp = functions.comp or function(x, y) return x < y end -- Function that returns true when correct index is found. local equals = functions.equals or function(x, y) return x == y end --	Initialize numbers. local iStart, iMid = 1, 0 local iEnd = next(t) and #t or require("Module:table").size(t) if iEnd == 0 then return nil end local iterations = 0 -- Do search. while iStart <= iEnd do		iterations = iterations + 1 -- Calculate middle. iMid = math.floor((iStart + iEnd) / 2) -- Get compare value. local value2 = getCompVal(t[iMid]) -- Return matching index. Assumes there are no duplicates. if equals(value, value2) then return iMid, iterations -- Keep searching. elseif comp(value, value2) then iEnd = iMid - 1 else iStart = iMid + 1 end end return nil, iterations end

local function comp(item1, item2) local type1, type2 = type(item1), type(item2) if type1 ~= type2 then return type1 < type2 else return item1 < item2 end end

local function getCode(val) if type(val) == "table" then return val.code else return val end end

local function getLanguageData(code) local i = export.binarySearch(		languageData,		code,		{ getCompVal = getCode, comp = comp }	) if i then return languageData[i] else error("No data for code " .. code .. ".") end end

local function forEach(t, func) for i, val in ipairs(t) do		func(val) end end

function export.show(frame) local codes = setmetatable({ "en", "fr", "de", "ka", "ru", "be" }, { __index = { forEach = forEach } }) local found = {} codes:forEach(		function(item)			table.insert(found, getLanguageData(item))		end	) return dump(found) .. dump(languageData) end

return export