Search code examples
searchlualua-table

Optimized Lua Table Searching


I have a LUA table:

local tableDatabase = {
  name = uniqueName,
  class = val_one_to_eight,   --not unique
  value = mostly_but_not_guaranteed_unique_int}

This table can be sorted by any of the above and may contain a very large data set.

Right now in order to insert I'm just iterating through the table ipairs until I find:

 insertedvalue.uniqueName > tableDatabase.uniqueName
 --(or comparing the other parms instead if they are the selected sort order.)

I need this function to work super fast. Is there a search algorithm someone could recommend for finding the index into the table to insert or some method I could use that would work on a lua table to optimize this for speed of insertions?


Solution

  • As I know, for strictly ordered structure you can use binary search or similar algorithms. Lua Users provides ready to use function.