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 1: Line 1:
local search = {}
+
local Search = {}
  
 
---------------
 
---------------
 
-- conductor --
 
-- conductor --
 
---------------
 
---------------
-- ancestry functionality (commented out)
+
-- sibling_key: optional, deprecate
-- sibling_key: optional, deprecate the whole thing (commented out)
+
function Search.conductor(query, tbl, sibling_key)
 +
  local f_name = "find"
 +
  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))
  
-- Possible query combinations:
+
   Search.state = {
--   query: {key, value}
 
--  query: {key, nil}
 
--  query: {nil, value}
 
--  query: key (string or number)
 
function search.conductor(query, tbl)
 
  assert(query, "conductor: missing argument #1 (query)")
 
  assert(tbl, "conductor: missing argument #2 (table to search through)")
 
  assert(type(tbl) == 'table', string.format("conductor: bad argument #2 (table expected, got %s)", type(tbl)))
 
 
 
  search.state = {
 
 
     ["args"] = {},
 
     ["args"] = {},
 
     ["queried"] = {}
 
     ["queried"] = {}
 
   }
 
   }
--~   search.ancestors = {}
+
   Search.ancestors = {}
 +
 
 +
  Search.state.args.query = query
 +
  Search.state.args.table = tbl
 +
  Search.state.args.sibling_key = sibling_key
  
   search.state.args.query = query
+
   local result
  search.state.args.table = tbl
 
--~  search.state.args.sibling_key = sibling_key
 
  
   local result = search.find(query, tbl)
+
   if not sibling_key then
 +
    result = Search.find_first(query, tbl)
 +
  else
 +
    result = Search.find_sibling(query, sibling_key, tbl)
 +
  end
  
 
   if result then
 
   if result then
--~     search.generate_anscestry_of_last_search(result)
+
     Search.generate_anscestry_of_last_search(result)
--~     result.ancestors = search.ancestors
+
     result.ancestors = Search.ancestors
 
     return result
 
     return result
 
   end
 
   end
Line 39: Line 38:
 
-- ancestry --
 
-- ancestry --
 
--------------
 
--------------
--~ function search.generate_anscestry_of_last_search(quad)
+
function Search.generate_anscestry_of_last_search(quad)
--~   quad = quad or {}
+
   quad = quad or {}
  
--~   for _,v in ipairs(search.state.queried) do
+
   for _,v in ipairs(Search.state.queried) do
--~     if v.value == quad.parent.table then
+
     if v.value == quad.parent.table then
--~       table.insert(search.ancestors, quad.parent.key)
+
       table.insert(Search.ancestors, quad.parent.key)
--~       search.generate_anscestry_of_last_search(v, search.state.queried, ancestors)
+
       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
--~   end
+
   end
--~ end
+
end
  
 
----------------------
 
----------------------
 
-- search_condition --
 
-- 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)
function search.search_condition(query, key, value)
 
 
   local condition = false
 
   local condition = false
  
   if type(query) == 'string' or type(query) == 'number' then
+
   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 70:
  
 
----------------
 
----------------
-- find first --
+
-- find_first --
 
----------------
 
----------------
search.meta = {}
+
function Search.find_first(query, tbl, tbl_key)
function search.find(query, tbl, parentKey)
+
   local subtables = {}
   local children = {}
 
  
   for k,v in pairs(tbl) do -- search through children that are not tables
+
   for k,v in pairs(tbl) do
  
 
     local quad = {
 
     local quad = {
Line 84: Line 81:
 
       value = v,
 
       value = v,
 
       parent = {
 
       parent = {
         key = parentKey,
+
         key = tbl_key,
 
         table = tbl
 
         table = tbl
 
       }
 
       }
 
     }
 
     }
  
--~     -- needed to generate ancestry
+
     -- I do this to be able to generate ancestry for the searh
--~     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
       search.meta = quad
+
       return quad
      return v
 
 
     elseif type(v) == "table" then
 
     elseif type(v) == "table" then
       table.insert(children, k)
+
       table.insert(subtables, k)
 
     end
 
     end
  
 
   end
 
   end
  
   for _,childK in ipairs(children) do -- search through child tables
+
  -- By doing it like this it will first search through all of the children and after that
     local found = search.find(query, tbl[childK], childK)
+
  -- descend into grandchildren.
     if found then return found end
+
   for _,k in ipairs(subtables) do
 +
    local f = Search.find_first(query, tbl[k], k)
 +
    if f then return f
 +
    end
 +
  end
 +
end
 +
 
 +
------------------
 +
-- find_sibling --
 +
------------------
 +
function Search.find_sibling(query, sibling_key, tbl, tbl_key)
 +
  local subtables = {}
 +
 
 +
  for k,v in pairs(tbl) do
 +
 
 +
    local quad = {
 +
      key = k,
 +
      value = v,
 +
      parent = {
 +
        key = tbl_key,
 +
        value = table
 +
      }
 +
    }
 +
 
 +
    -- I do this to be able to generate ancestry for the searh
 +
    table.insert(Search.state.queried, quad)
 +
 
 +
    if Search.search_condition(query, k, v) and tbl[sibling_key] then
 +
      local sibling_quad = {
 +
        key = sibling_key,
 +
        value = tbl[sibling_key],
 +
        parent = {
 +
          key = tbl_key,
 +
          table = tbl
 +
        }
 +
      }
 +
      table.insert(Search.state.queried, sibling_quad)
 +
      return sibling_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_sibling(query, sibling_key, tbl[k], k)
 +
     if f then return f
 +
    end
 
   end
 
   end
 
end
 
end
  
return search -- return module
+
return Search

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)