From 2128dd4c15bcd6d94e0f14a60e2be06c10c5a1f6 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sun, 19 Nov 2023 01:23:57 +0100 Subject: [PATCH] Add basic table ordered keys iterator --- mods/CORE/mcl_util/init.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index e894eebd7..17c4bfc14 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -22,6 +22,30 @@ function table.update_nil(t, ...) return t end +---Works the same as `pairs`, but order returned by keys +--- +---Taken from https://www.lua.org/pil/19.3.html +---@generic T: table, K, V, C +---@param t T +---@param f? fun(a: C, b: C):boolean +---@return fun():K, V +function table.pairs_by_keys(t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + + local i = 0 -- iterator variable + local function iter() -- iterator function + i = i + 1 + if a[i] == nil then + return nil + else + return a[i], t[a[i]] + end + end + return iter +end + local LOGGING_ON = minetest.settings:get_bool("mcl_logging_default", false) local LOG_MODULE = "[MCL2]" function mcl_util.mcl_log(message, module, bypass_default_logger)