From 4d3cf18797729762b642c942a418de8ed0b117ec Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Tue, 6 Jun 2017 00:24:24 +0200 Subject: [PATCH] Refactored `analyse_structure`, and improved the quality of gotten depths. --- analyze_structure.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/analyze_structure.lua b/analyze_structure.lua index c9f49fa..4d7a900 100644 --- a/analyze_structure.lua +++ b/analyze_structure.lua @@ -159,6 +159,7 @@ local function get_table_info (t) local key_types = get_key_types(t) local info = {} + info.address = tostring(t):sub(8) info.nr_elems = nr_elements_in_table(t) info.seq_elems, info.has_holes = nr_elements_in_seq(t) info.map_elems = info.nr_elems - info.seq_elems @@ -191,21 +192,25 @@ local function analyze_structure (root) queue.bottom = queue.bottom + 1 local node = queue[queue.bottom-1] -- Who've been visited? Bookkeeping - visited[node] = (visited[node] or 0) + 1 + visited[node], info[node] = (visited[node] or 0) + 1, info[node] or get_table_info(node) if visited[node] == 2 then info[node].marker, next_mark = next_mark, next_mark + 1 end -- Get table info & visit children. if visited[node] < 2 then - info[node] = get_table_info(node) - info[node].depth = depth[node] for k, v in pairs(node) do - if type(k) == 'table' then queue[queue.top], queue.top, depth[k] = k, queue.top + 1, depth[node] + 1 end - if type(v) == 'table' then queue[queue.top], queue.top, depth[v] = v, queue.top + 1, depth[node] + 1 end + if type(k) == 'table' then queue[queue.top], queue.top, depth[k] = k, queue.top + 1, math.min(depth[k] or math.huge, depth[node] + 1) end + if type(v) == 'table' then queue[queue.top], queue.top, depth[v] = v, queue.top + 1, math.min(depth[v] or math.huge, depth[node] + 1) end end end end + -- Use depth collected + for node in pairs(depth) do + info[node].depth = depth[node] + info[node].address = info[node].address .. ': ' .. info[node].depth + end + assert(type(info) == 'table') return info end