Difference between revisions of "Module:Test/lib/search"
Jump to navigation
Jump to search
m |
(removed surplus) |
||
Line 1: | Line 1: | ||
− | local | + | local search = {} |
--------------- | --------------- | ||
Line 7: | Line 7: | ||
-- query: {key, value} | -- query: {key, value} | ||
-- query: {key, } | -- query: {key, } | ||
− | -- query: {, value} | + | -- query: {nil, value} |
-- query: key (string or number) | -- query: key (string or number) | ||
− | function | + | function search.conductor(query, tbl) |
− | local f_name = " | + | local f_name = "conductor" |
assert(query, string.format("bad argument #1 to '%s' (argument missing, search query)", f_name)) | assert(query, string.format("bad argument #1 to '%s' (argument missing, search query)", f_name)) | ||
assert(tbl, string.format("bad argument #2 to '%s' (argument missing, table to search through)", f_name)) | assert(tbl, string.format("bad argument #2 to '%s' (argument missing, table to search through)", f_name)) | ||
− | + | search.state = { | |
["args"] = {}, | ["args"] = {}, | ||
["queried"] = {} | ["queried"] = {} | ||
} | } | ||
− | + | search.ancestors = {} | |
− | + | search.state.args.query = query | |
− | + | search.state.args.table = tbl | |
− | + | --~ search.state.args.sibling_key = sibling_key | |
− | local | + | local result = search.find_first(query, tbl) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if result then | if result then | ||
− | + | --~ search.generate_anscestry_of_last_search(result) | |
− | result.ancestors = | + | --~ result.ancestors = search.ancestors |
return result | return result | ||
end | end | ||
Line 42: | Line 36: | ||
-- ancestry -- | -- ancestry -- | ||
-------------- | -------------- | ||
− | function | + | function search.generate_anscestry_of_last_search(quad) |
quad = quad or {} | quad = quad or {} | ||
− | for _,v in ipairs( | + | for _,v in ipairs(search.state.queried) do |
if v.value == quad.parent.table then | if v.value == quad.parent.table then | ||
− | table.insert( | + | table.insert(search.ancestors, quad.parent.key) |
− | + | search.generate_anscestry_of_last_search(v, search.state.queried, ancestors) | |
elseif not quad.parent.key then return nil | elseif not quad.parent.key then return nil | ||
end | end | ||
Line 57: | Line 51: | ||
-- search_condition -- | -- search_condition -- | ||
---------------------- | ---------------------- | ||
− | function | + | -- note: if I want to search for true/false, will need to change this up a bit |
+ | function search.search_condition(query, key, value) | ||
local condition = false | local condition = false | ||
Line 76: | Line 71: | ||
-- find_first -- | -- find_first -- | ||
---------------- | ---------------- | ||
− | function | + | function search.find_first(query, tbl, tbl_key) |
local subtables = {} | local subtables = {} | ||
Line 91: | Line 86: | ||
-- I do this to be able to generate ancestry for the searh | -- I do this to be able to generate ancestry for the searh | ||
− | table.insert( | + | -- functionality disabled at the moment |
+ | --~ table.insert(search.state.queried, quad) | ||
− | if | + | if search.search_condition(query, k, v) then |
return quad | return quad | ||
elseif type(v) == "table" then | elseif type(v) == "table" then | ||
Line 104: | Line 100: | ||
-- descend into grandchildren. | -- descend into grandchildren. | ||
for _,k in ipairs(subtables) do | for _,k in ipairs(subtables) do | ||
− | local f = | + | local f = search.find_first(query, tbl[k], k) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if f then return f | if f then return f | ||
end | end | ||
Line 156: | Line 106: | ||
end | end | ||
− | return | + | return search |
Revision as of 15:19, 22 April 2021
This page has been marked as needing documentation of its function and purpose. You can help RimWorld Wiki by creating it here |
local search = {} --------------- -- conductor -- --------------- -- sibling_key: optional, deprecate the whole thing -- query: {key, value} -- query: {key, } -- query: {nil, value} -- query: key (string or number) function search.conductor(query, tbl) local f_name = "conductor" assert(query, string.format("bad argument #1 to '%s' (argument missing, search query)", f_name)) assert(tbl, string.format("bad argument #2 to '%s' (argument missing, table to search through)", f_name)) search.state = { ["args"] = {}, ["queried"] = {} } search.ancestors = {} search.state.args.query = query search.state.args.table = tbl --~ search.state.args.sibling_key = sibling_key local result = search.find_first(query, tbl) if result then --~ search.generate_anscestry_of_last_search(result) --~ result.ancestors = search.ancestors return result end end -------------- -- ancestry -- -------------- function search.generate_anscestry_of_last_search(quad) quad = quad or {} for _,v in ipairs(search.state.queried) do if v.value == quad.parent.table then table.insert(search.ancestors, quad.parent.key) search.generate_anscestry_of_last_search(v, search.state.queried, ancestors) elseif not quad.parent.key then return nil end end end ---------------------- -- search_condition -- ---------------------- -- note: if I want to search for true/false, will need to change this up a bit function search.search_condition(query, key, value) local condition = false if type(query) == "string" or type(query) == "number" then condition = key == query elseif query[1] and query[2] then condition = key == query[1] and value == query[2] elseif query[1] then condition = key == query[1] elseif query[2] then condition = value == query[2] end return condition end ---------------- -- find_first -- ---------------- function search.find_first(query, tbl, tbl_key) local subtables = {} for k,v in pairs(tbl) do local quad = { key = k, value = v, parent = { key = tbl_key, table = tbl } } -- I do this to be able to generate ancestry for the searh -- functionality disabled at the moment --~ table.insert(search.state.queried, quad) if search.search_condition(query, k, v) then return quad elseif type(v) == "table" then table.insert(subtables, k) end end -- By doing it like this it will first search through all of the children and after that -- descend into grandchildren. for _,k in ipairs(subtables) do local f = search.find_first(query, tbl[k], k) if f then return f end end end return search