mNo edit summary |
m (Speed test) |
||
(12 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
local bit32 = require( 'bit32' ) |
local bit32 = require( 'bit32' ) |
||
− | local w_bit = require( 'module:bit' ) |
+ | local w_bit = require( 'module:bit' ) |
local w_frame = require( 'module:frame' ) |
local w_frame = require( 'module:frame' ) |
||
Line 21: | Line 21: | ||
function p.find( f ) |
function p.find( f ) |
||
− | local args = w_frame.args( f ) |
+ | local args = w_frame.args( f ) |
+ | local entries = mw.text.split( args[1], '▼', true ) |
||
+ | if #entries < 2 then |
||
+ | return mw.text.split( entries[1], '►', true )[2] |
||
+ | end |
||
local filter = args[2] == 'last' and 8 or args[2] or tonumber( mw.getCurrentFrame():callParserFunction( '#var:context' ) ) or 15 |
local filter = args[2] == 'last' and 8 or args[2] or tonumber( mw.getCurrentFrame():callParserFunction( '#var:context' ) ) or 15 |
||
local parts |
local parts |
||
− | for |
+ | for _, entry in ipairs( entries ) do |
− | parts = mw.text.split( |
+ | parts = mw.text.split( entry, '►' ) |
− | if |
+ | if bit32.band( parts[1], filter ) > 0 then |
return parts[2] |
return parts[2] |
||
end |
end |
||
end |
end |
||
+ | end |
||
+ | |||
+ | |||
+ | function p.parse_dict( f ) |
||
+ | local args = w_frame.args( f ) |
||
+ | local vals = {} |
||
+ | for str in mw.text.gsplit( args[1], ',' ) do |
||
+ | pos = string.find( str, ':' ) |
||
+ | if pos then |
||
+ | k = mw.text.trim( string.sub( str, 1, pos - 1 ) ) |
||
+ | v = mw.text.trim( string.sub( str, pos + 1 ) ) |
||
+ | else |
||
+ | k = '' |
||
+ | v = mw.text.trim( str ) |
||
+ | end |
||
+ | if not args['keys parsed'] then |
||
+ | k = data.values[k] |
||
+ | else |
||
+ | k = tonumber( k ) |
||
+ | end |
||
+ | k = k or 0 |
||
+ | table.insert(vals, { c = w_bit.count{ k }, k = k, v = v }) |
||
+ | end |
||
+ | |||
+ | table.sort( vals, function (x, y) return x.c < y.c end ) |
||
+ | mask = vals[1].k |
||
+ | new_vals = { vals[1] } |
||
+ | exists = { [vals[1].v] = 1 } |
||
+ | j = 1 |
||
+ | for i = 2, #vals do |
||
+ | vals[i].k = bit32.band( vals[i].k, bit32.bnot( mask ) ) |
||
+ | mask = bit32.bor( vals[i].k, mask ) |
||
+ | if exists[vals[i].v] then |
||
+ | j_dup = exists[vals[i].v] |
||
+ | new_vals[j_dup].k = bit32.bor( new_vals[j_dup].k, vals[i].k ) |
||
+ | else |
||
+ | j = j + 1 |
||
+ | new_vals[j] = vals[i] |
||
+ | exists[vals[i].v] = j |
||
+ | end |
||
+ | end |
||
+ | |||
+ | result = {} |
||
+ | for i = 1, #new_vals do |
||
+ | result[i] = new_vals[i].k .. '►' .. new_vals[i].v |
||
+ | end |
||
+ | return table.concat( result, '▼', i, j ) |
||
end |
end |
||
Latest revision as of 11:43, 27 September 2023
local p = {}
local bit32 = require( 'bit32' )
local w_bit = require( 'module:bit' )
local w_frame = require( 'module:frame' )
local data = mw.loadData( 'module:dlc/data' )
function p.parse( f )
return data.values[w_frame.args( f )[1] or ''] or 0
end
function p.test( f )
local args = w_frame.args( f )
return ( data.test[tonumber( args[1] )] or {} )[tonumber( args[2] ) or 15] or 0
end
function p.find( f )
local args = w_frame.args( f )
local entries = mw.text.split( args[1], '▼', true )
if #entries < 2 then
return mw.text.split( entries[1], '►', true )[2]
end
local filter = args[2] == 'last' and 8 or args[2] or tonumber( mw.getCurrentFrame():callParserFunction( '#var:context' ) ) or 15
local parts
for _, entry in ipairs( entries ) do
parts = mw.text.split( entry, '►' )
if bit32.band( parts[1], filter ) > 0 then
return parts[2]
end
end
end
function p.parse_dict( f )
local args = w_frame.args( f )
local vals = {}
for str in mw.text.gsplit( args[1], ',' ) do
pos = string.find( str, ':' )
if pos then
k = mw.text.trim( string.sub( str, 1, pos - 1 ) )
v = mw.text.trim( string.sub( str, pos + 1 ) )
else
k = ''
v = mw.text.trim( str )
end
if not args['keys parsed'] then
k = data.values[k]
else
k = tonumber( k )
end
k = k or 0
table.insert(vals, { c = w_bit.count{ k }, k = k, v = v })
end
table.sort( vals, function (x, y) return x.c < y.c end )
mask = vals[1].k
new_vals = { vals[1] }
exists = { [vals[1].v] = 1 }
j = 1
for i = 2, #vals do
vals[i].k = bit32.band( vals[i].k, bit32.bnot( mask ) )
mask = bit32.bor( vals[i].k, mask )
if exists[vals[i].v] then
j_dup = exists[vals[i].v]
new_vals[j_dup].k = bit32.bor( new_vals[j_dup].k, vals[i].k )
else
j = j + 1
new_vals[j] = vals[i]
exists[vals[i].v] = j
end
end
result = {}
for i = 1, #new_vals do
result[i] = new_vals[i].k .. '►' .. new_vals[i].v
end
return table.concat( result, '▼', i, j )
end
return p