Module:MOS tunings: Difference between revisions

Ganaram inukshuk (talk | contribs)
Simplified step ratio sorting
Ganaram inukshuk (talk | contribs)
m Undo revision 156351 by Ganaram inukshuk (talk) go back to previous implementation to resolve later
Tag: Undo
Line 21: Line 21:


-- Helper function
-- Helper function
-- Given two ratios, return whether the first is less than the second. To be
-- Sorts step ratios L:s by their hardnesses
-- used as part of step ratio preprocessing by being passed into table.sort.
function p.sort_step_ratios(step_ratios)
function p.step_ratio_comparison_function(step_ratio_1, step_ratio_2)
if #step_ratios < 2 then
local hardness_1 = step_ratio_1[1] / step_ratio_1[2]
return step_ratios
local hardness_2 = step_ratio_2[1] / step_ratio_2[2]
end
return hardness_1 < hardness_2
-- Sort using selection sort, which is ok for smol datasets.
for i = 1, #step_ratios - 1 do
local index_of_smallest = i
local current_val = step_ratios[i][1] / step_ratios[i][2]
-- Find the ratio with the smallest hardness
for j = i + 1, #step_ratios do
if (step_ratios[j][1] / step_ratios[j][2] < current_val) then
index_of_smallest = j
end
end
if index_of_smallest ~= i then
local temp = step_ratios[index_of_smallest]
step_ratios[index_of_smallest] = step_ratios[i]
step_ratios[i] = temp
end
end
return step_ratios
end
end


Line 32: Line 51:
-- Finds the step ratio range and sorts step ratios
-- Finds the step ratio range and sorts step ratios
function p.preprocess_step_ratios(step_ratios)
function p.preprocess_step_ratios(step_ratios)
local step_ratios = table.sort(step_ratios, step_ratio_comparison_function)
local step_ratios = p.sort_step_ratios(step_ratios)
local step_ratio_range = ""
local step_ratio_range = ""