From b5e7f331c4e6b48130351569040ece457fd2b258 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 7 Jul 2021 17:32:33 +0200 Subject: [PATCH] Replace count_values with count_objects --- luon.lua | 25 +++++++++++++------------ table.lua | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/luon.lua b/luon.lua index 3ae43f7..e1ca00b 100644 --- a/luon.lua +++ b/luon.lua @@ -1,6 +1,7 @@ local assert, next, pairs, pcall, error, type, table_insert, table_concat, string_format, string_match, setfenv, math_huge, loadfile, loadstring = assert, next, pairs, pcall, error, type, table.insert, table.concat, string.format, string.match, setfenv, math.huge, loadfile, loadstring -local count_values = modlib.table.count_values + +local count_objects = modlib.table.count_objects -- Build a table with the succeeding character from A-Z local succ = {} @@ -15,7 +16,7 @@ end local _ENV = {} setfenv(1, _ENV) -function write(object, write) +function write(value, write) local reference = {"A"} local function increment_reference(place) if not reference[place] then @@ -29,9 +30,9 @@ function write(object, write) end local references = {} local to_fill = {} - for value, count in pairs(count_values(object)) do + for value, count in pairs(count_objects(value)) do local type_ = type(value) - if count >= 2 and ((type_ == "string" and #reference + 2 >= #value) or type_ == "table") then + if count >= 2 and (type_ ~= "string" or #reference + 2 >= #value) then local ref = table_concat(reference) write(ref) write"=" @@ -116,28 +117,28 @@ function write(object, write) error("unsupported type: " .. type_) end end - local fill_root = to_fill[object] + local fill_root = to_fill[value] if fill_root then -- Root table is circular, must return by named reference - dump(object) + dump(value) write"return " - write(references[object]) + write(references[value]) else -- Root table is not circular, can directly start writing write"return " - dump(object) + dump(value) end end -function write_file(object, file) - return write(object, function(text) +function write_file(value, file) + return write(value, function(text) file:write(text) end) end -function write_string(object) +function write_string(value) local rope = {} - write(object, function(text) + write(value, function(text) table_insert(rope, text) end) return table_concat(rope) diff --git a/table.lua b/table.lua index 3c3c200..6dab63a 100644 --- a/table.lua +++ b/table.lua @@ -337,24 +337,26 @@ function deep_foreach_any(table, func) visit(table) end --- Recursively counts occurences of values in a table --- Also counts primitive values like boolean and number --- Does not count NaN, because that doesn't work as a table index -function count_values(value) +-- Recursively counts occurences of objects (non-primitives including strings) in a table. +function count_objects(value) local counts = {} - local function count_values_(value) - -- Ignore NaN - if value ~= value then return end + if value == nil then + -- Early return for nil + return counts + end + local function count_values(value) + local type_ = type(value) + if type_ == "boolean" or type_ == "number" then return end local count = counts[value] counts[value] = (count or 0) + 1 - if not count and type(value) == "table" then + if not count and type_ == "table" then for k, v in pairs(value) do - count_values_(k) - count_values_(v) + count_values(k) + count_values(v) end end end - count_values_(value) + count_values(value) return counts end