-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_tables.lua
More file actions
73 lines (64 loc) · 1.98 KB
/
lib_tables.lua
File metadata and controls
73 lines (64 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
local m_log, app_name = ...
local M = {}
M.m_log = m_log
M.app_name = app_name
function M.tprint(t, s)
for k, v in pairs(t) do
local kfmt = '["' .. tostring(k) .. '"]'
if type(k) ~= 'string' then
kfmt = '[' .. k .. ']'
end
local vfmt = '"' .. tostring(v) .. '"'
if type(v) == 'table' then
M.tprint(v, (s or '') .. kfmt)
else
if type(v) ~= 'string' then
vfmt = tostring(v)
end
M.m_log.info(type(t) .. (s or '') .. kfmt .. ' = ' .. vfmt)
end
end
end
function M.table_clear(tbl)
-- clean without creating a new list
for i = 0, #tbl do
table.remove(tbl, 1)
end
end
function M.table_print(prefix, tbl)
M.m_log.info(">>> table_print (%s)", prefix)
for i = 1, #tbl, 1 do
local val = tbl[i]
if type(val) ~= "table" then
M.m_log.info(string.format("%d. %s: %s", i, prefix, val))
else
local t_val = val
M.m_log.info("-++++------------ %d %s", #val, type(t_val))
for j = 1, #t_val, 1 do
local val = t_val[j]
M.m_log.info(string.format("%d. %s: %s", i, prefix, val))
end
end
end
M.m_log.info("<<< table_print end (%s) ", prefix)
end
function M.list_ordered_insert(lst, newVal, cmp, firstValAt)
-- sort
for i = firstValAt, #lst, 1 do
-- remove duplication
--M.m_log.info("list_ordered_insert - %s ? %s", newVal, lst[i] )
if newVal == lst[i] then
--M.table_print("list_ordered_insert - duplicated", lst)
return
end
if cmp(newVal, lst[i]) == true then
table.insert(lst, i, newVal)
--M.table_print("list_ordered_insert - inserted", lst)
return
end
--M.table_print("list_ordered_insert-loop", lst)
end
table.insert(lst, newVal)
--M.table_print("list_ordered_insert-inserted-to-end", lst)
end
return M