Editing Module:Test/lib/search
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 4: | Line 4: | ||
-- conductor -- | -- conductor -- | ||
--------------- | --------------- | ||
− | + | -- sibling_key: optional, deprecate the whole thing | |
− | -- 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) | function search.conductor(query, tbl) | ||
− | + | local f_name = "conductor" | |
− | assert( | + | assert(query, string.format("bad argument #1 to '%s' (argument missing, search query)", f_name)) |
− | assert | + | assert(tbl, string.format("bad argument #2 to '%s' (argument missing, table to search through)", f_name)) |
search.state = { | search.state = { | ||
Line 21: | Line 18: | ||
["queried"] = {} | ["queried"] = {} | ||
} | } | ||
− | + | search.ancestors = {} | |
search.state.args.query = query | search.state.args.query = query | ||
Line 27: | Line 24: | ||
--~ search.state.args.sibling_key = sibling_key | --~ search.state.args.sibling_key = sibling_key | ||
− | local result = search. | + | local result = search.find_first(query, tbl) |
if result then | if result then | ||
Line 39: | Line 36: | ||
-- ancestry -- | -- 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 | |
---------------------- | ---------------------- | ||
Line 58: | Line 55: | ||
local condition = false | local condition = false | ||
− | if type(query) == | + | if type(query) == "string" or type(query) == "number" then |
condition = key == query | condition = key == query | ||
elseif query[1] and query[2] then | elseif query[1] and query[2] then | ||
Line 72: | Line 69: | ||
---------------- | ---------------- | ||
− | -- | + | -- find_first -- |
---------------- | ---------------- | ||
− | + | function search.find_first(query, tbl, tbl_key) | |
− | function search. | + | local subtables = {} |
− | local | ||
− | for k,v in pairs(tbl) do | + | for k,v in pairs(tbl) do |
local quad = { | local quad = { | ||
Line 84: | Line 80: | ||
value = v, | value = v, | ||
parent = { | parent = { | ||
− | key = | + | key = tbl_key, |
table = tbl | 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) | --~ table.insert(search.state.queried, quad) | ||
if search.search_condition(query, k, v) then | if search.search_condition(query, k, v) then | ||
− | + | return quad | |
− | |||
elseif type(v) == "table" then | elseif type(v) == "table" then | ||
− | table.insert( | + | table.insert(subtables, k) |
end | end | ||
end | end | ||
− | for _, | + | -- By doing it like this it will first search through all of the children and after that |
− | local | + | -- descend into grandchildren. |
− | if | + | for _,k in ipairs(subtables) do |
+ | local f = search.find_first(query, tbl[k], k) | ||
+ | if f then return f | ||
+ | end | ||
end | end | ||
end | end | ||
− | return search | + | return search |