From 628779b5a2357685221f62141bec22c0e84a24f1 Mon Sep 17 00:00:00 2001 From: Hisham Date: Thu, 28 Jul 2016 18:15:13 -0300 Subject: Move sortedpairs to core; used by luarocks.loader. --- src/luarocks/core/util.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/luarocks/util.lua | 62 --------------------------------------------- 2 files changed, 63 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 4f896b17..f2e409d1 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua @@ -198,5 +198,68 @@ function util.printerr(...) io.stderr:write("\n") end +--- Simple sort function used as a default for util.sortedpairs. +local function default_sort(a, b) + local ta = type(a) + local tb = type(b) + if ta == "number" and tb == "number" then + return a < b + elseif ta == "number" then + return true + elseif tb == "number" then + return false + else + return tostring(a) < tostring(b) + end +end + +--- The iterator function used internally by util.sortedpairs. +-- @param tbl table: The table to be iterated. +-- @param sort_function function or nil: An optional comparison function +-- to be used by table.sort when sorting keys. +-- @see sortedpairs +local function sortedpairs_iterator(tbl, sort_function) + local ks = util.keys(tbl) + if not sort_function or type(sort_function) == "function" then + table.sort(ks, sort_function or default_sort) + for _, k in ipairs(ks) do + coroutine.yield(k, tbl[k]) + end + else + local order = sort_function + local done = {} + for _, k in ipairs(order) do + local sub_order + if type(k) == "table" then + sub_order = k[2] + k = k[1] + end + if tbl[k] then + done[k] = true + coroutine.yield(k, tbl[k], sub_order) + end + end + table.sort(ks, default_sort) + for _, k in ipairs(ks) do + if not done[k] then + coroutine.yield(k, tbl[k]) + end + end + end +end + +--- A table iterator generator that returns elements sorted by key, +-- to be used in "for" loops. +-- @param tbl table: The table to be iterated. +-- @param sort_function function or table or nil: An optional comparison function +-- to be used by table.sort when sorting keys, or an array listing an explicit order +-- for keys. If a value itself is an array, it is taken so that the first element +-- is a string representing the field name, and the second element is a priority table +-- for that key. +-- @return function: the iterator function. +function util.sortedpairs(tbl, sort_function) + return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) +end + return util diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index f41b1c38..9760fafd 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -292,68 +292,6 @@ function util.variable_substitutions(tbl, vars) end end -local function default_sort(a, b) - local ta = type(a) - local tb = type(b) - if ta == "number" and tb == "number" then - return a < b - elseif ta == "number" then - return true - elseif tb == "number" then - return false - else - return tostring(a) < tostring(b) - end -end - --- The iterator function used internally by util.sortedpairs. --- @param tbl table: The table to be iterated. --- @param sort_function function or nil: An optional comparison function --- to be used by table.sort when sorting keys. --- @see sortedpairs -local function sortedpairs_iterator(tbl, sort_function) - local ks = util.keys(tbl) - if not sort_function or type(sort_function) == "function" then - table.sort(ks, sort_function or default_sort) - for _, k in ipairs(ks) do - coroutine.yield(k, tbl[k]) - end - else - local order = sort_function - local done = {} - for _, k in ipairs(order) do - local sub_order - if type(k) == "table" then - sub_order = k[2] - k = k[1] - end - if tbl[k] then - done[k] = true - coroutine.yield(k, tbl[k], sub_order) - end - end - table.sort(ks, default_sort) - for _, k in ipairs(ks) do - if not done[k] then - coroutine.yield(k, tbl[k]) - end - end - end -end - ---- A table iterator generator that returns elements sorted by key, --- to be used in "for" loops. --- @param tbl table: The table to be iterated. --- @param sort_function function or table or nil: An optional comparison function --- to be used by table.sort when sorting keys, or an array listing an explicit order --- for keys. If a value itself is an array, it is taken so that the first element --- is a string representing the field name, and the second element is a priority table --- for that key. --- @return function: the iterator function. -function util.sortedpairs(tbl, sort_function) - return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) -end - function util.lua_versions() local versions = { "5.1", "5.2", "5.3" } local i = 0 -- cgit v1.2.3-55-g6feb