モジュール:Statistical

local p = {}

local Regions = mw.loadData("モジュール:Statistical/Regions")
bit32 = require( 'bit32' )

	local function LimitDouble(Val)
	    local MaxNumber = 2147483648
	return Val - (math.floor(Val / MaxNumber) * MaxNumber)
	end
	
	local function shl(Val, Shift)
	    if Shift > 0 then
	    return  LimitDouble(Val * (2 ^ Shift))
	    else
	    return Value
	    end 
	end
	local function shr(Val, Shift)
	    if Shift > 0 then 
		return math.floor(Val / (2 ^ Shift))
	    else
		return Val
	    end
	end
	local function MakeHash(PlaceName)    
		local dataLength = mw.ustring.len(PlaceName)
	if dataLength == 0 then return 0 end
		local hash = dataLength
		local remainingBytes =  math.fmod(dataLength, 2)
	    local numberOfLoops = math.floor(dataLength / 2)
	    local currentIndex = 0
	local tmp = 0
	while (numberOfLoops > 0) do 
			hash = LimitDouble(hash + mw.ustring.codepoint(PlaceName, currentIndex + 1))
			tmp = bit32.bxor(shl(mw.ustring.codepoint(PlaceName, currentIndex + 2), 11), hash)
			hash = bit32.bxor(shl(hash, 16), tmp)
			hash = LimitDouble(hash + shr(hash, 11))
			currentIndex = currentIndex + 2
			numberOfLoops = numberOfLoops - 1
	    end
		if remainingBytes == 1 then
			hash = LimitDouble(hash + mw.ustring.codepoint(PlaceName, currentIndex + 1))
			hash = bit32.bxor(hash, shl(hash, 10))
			hash = LimitDouble(hash + shr(hash, 1))
	    end
		hash = bit32.bxor(hash, shl(hash, 3))
		hash = LimitDouble(hash + shr(hash, 5))
		hash = bit32.bxor(hash, shl(hash, 4))
		hash = LimitDouble(hash + shr(hash, 17))
		hash = bit32.bxor(hash, shl(hash, 25))
		hash = LimitDouble(hash + shr(hash, 6))
		return hash
	end

local function First_less_Second(a, b)  
local LenA = mw.ustring.len(a)
local LenB = mw.ustring.len(b)
for i = 1, (LenA  a_groupMinLength) then
			needsGrouping = true;
		end
	else
		-- decimal point present
		if ((decimalPosition > a_groupMinLength) or (((mw.ustring.len(a_number) - decimalPosition) > DIGIT_GROUPING_SIZE) and (not a_groupOnlyIntegerPart))) then
			needsGrouping = true;
		end
		-- replace the decimal point
		a_number = mw.ustring.sub(a_number, 1, decimalPosition - 1) .. a_decimalMark .. mw.ustring.sub(a_number, decimalPosition + 1);
	end
	if (needsGrouping and (decimalPosition > DIGIT_GROUPING_SIZE + 1)) then
		-- grouping of integer part necessary
		local i = decimalPosition - DIGIT_GROUPING_SIZE;
		while (i > 1) do
			-- group the integer part
			a_number = mw.ustring.sub(a_number, 1, i - 1) .. a_groupMark .. mw.ustring.sub(a_number, i);
			decimalPosition = decimalPosition + mw.ustring.len(a_groupMark);
			i = i - DIGIT_GROUPING_SIZE;
		end
	end
	-- skip to the end of the new decimal mark (in case it is more than one char)
	decimalPosition = decimalPosition + mw.ustring.len(a_decimalMark) - 1;
	if (a_groupOnlyIntegerPart) then
		needsGrouping = false;
	end
	if (needsGrouping and ((mw.ustring.len(a_number) - decimalPosition) > DIGIT_GROUPING_SIZE)) then
		-- grouping of fractional part necessary
		-- using negative numbers (index from the end of the string)
		local i = decimalPosition - mw.ustring.len(a_number) + DIGIT_GROUPING_SIZE;
		while (i  add a 0 to the beginning
			number = "0" .. number;
		else
			if (mw.ustring.sub(number, -1) == ".") then
				-- number ends with "." -> remove it
				number = mw.ustring.sub(number, 1, -2);
			end
		end
		if ((number == mw.ustring.match(number, "^%d+$")) or (number == mw.ustring.match(number, "^%d+%.%d+$"))) then
			-- number has valid format (only digits or digits.digits) -> format it and add sign (if any) again
			number = sign .. internalFormatNumber(number, format.decimalMark, format.groupMark, format.groupMinLength, format.groupOnlyIntegerPart);
		else
			-- number has no valid format -> undo all modifications
			number = a_frame.args"number";
		end
	end	   
	return number;
end

function GetHashData(PlaceHash)     
	local NumPage = math.floor((PlaceHash - 1) / 33554432 + 2)
	if NumPage == 2 and (PlaceHash - 1)  Данные не обнаружены. Возможно, страница переименовывалась. Проверьте справочник"..  ((args.nocat and "") or "Категория:Википедия:Статьи с неправильными параметрами шаблона Население")}} 
	end
	end
	if check then
		return 1;
	end
local LastRecord = 0
for k in pairs(PlaceData) do LastRecord = LastRecord + 1 end
local NumRecord = LastRecord

	local function FormatY()    
	    return PlaceDataNumRecord1
	end

	local function FormatN()    
	    return PlaceDataNumRecord2
	end

	local function FormatS(SourceType)  
	    if PlaceDataNumRecord3 == "" then
	        return ""
	    else
	        local Source1
	        local Source2 
	        if string.find(PlaceDataNumRecord3,"%d+A-Z+")==1 then
	            Source1 = RegionData'Источники'PlaceDataNumRecord31
	            Source2 = PlaceDataNumRecord3
	        else
	            Source1 = PlaceDataNumRecord3
	            Source2 = ""
	        end
	        if string.find(Source1, "https?://")==1 then
	            Source1 = ..Source1..
	        end
			if SourceType == "и" then
				return Source1
			end
	        if Source2 == "" then
	            return frame:callParserFunction{name = '#tag:ref', args = {Source1}}
	        else
	            return frame:callParserFunction{name = '#tag:ref', args = {Source1, name = Source2}}
	        end
	    end
	end        

	local function FormatF()    
	    return formatNum(PlaceDataNumRecord2)    
	end

	local function FormatT()    
	    if NumRecord > 1 then
	        if PlaceDataNumRecord2 > PlaceDataNumRecord - 12 then
	            return "↗" 
	        elseif PlaceDataNumRecord2 ↘"  
	        else
	            return "→"  
	        end
	    else
	        return ""
	    end            
	end

if Format == 'Год' or Format == 'г' then
return FormatY()    
elseif Format == 'Безформат' or Format == 'Число' or Format == 'ч'  then
return FormatN()    
elseif Format == 'Ссылка' or Format == 'с'  then
return FormatS("с")    
elseif Format == 'Источник' or Format == 'и'  then
return FormatS("и")    
elseif Format == 'Формат' or Format == 'ф'  then
return FormatF()    
elseif Format == 'ФорматГод' or Format == 'фг'  then
return FormatF().." ("..FormatY()..")"    
elseif Format == 'ФорматСсылка' or Format == 'фс'  then
return FormatF()..FormatS()    
elseif Format == 'ФорматСсылкаГод' or Format == 'фсг'  then
return FormatF()..FormatS().." ("..FormatY()..")"
elseif Format == 'Тренд' or Format == 'т'  then
return FormatT()..FormatF()    
elseif Format == 'Значение' or Format == 'ТрендСсылка' or Format == 'тс'  then
return FormatT()..FormatF()..FormatS()
elseif Format == 'ТрендСсылкаГод' or Format == 'тсг'  then
return FormatT()..FormatF()..FormatS().." ("..FormatY()..")"
elseif Format == 'Хеш' or Format == 'х'  then
return FormatH()
	elseif Format == 'Диаграмма' or Format == 'д'  then
		local tempHeight = 320
		local tempWidth = 800
		local tempMod = math.fmod (LastRecord, 5)
		if LastRecord  LastRecord then Column = LastRecord end

if args'Оформление' ~= nil then
HTML:attr('class', args'Оформление')
else
HTML:attr('class', 'wikitable') 
end

local TempRow
local NumRow = 0
TempRow = HTML:tag('th'):attr('colspan', Column):wikitext(args'Заголовок' or '人口 ')
for i = 1, math.ceil(LastRecord / Column) do
TempRow = HTML:tag('tr'):addClass("bright")
for j = 1, Column do
				NumRecord = (i - 1) * Column + j
if PlaceDataNumRecord == nil then
TempRow:tag('th'):wikitext("")
else
TempRow:tag('th'):wikitext(FormatY()..FormatS("с"))
end
end

TempRow = HTML:tag('tr'):attr('align', 'center')
for j = 1, Column do
				NumRecord = (i - 1) * Column + j
if PlaceDataNumRecord == nil then
TempRow:tag('td'):wikitext("")
else
TempRow:tag('td'):wikitext(FormatT()..FormatF())
end
end
end            
return tostring(HTML)
end
return 1, Format
end

return p