Search code examples
arraysluacomplexity-theorycomputercraft

Time complexity of array manipulations in LUA


I'm discovering Lua with ComputerCraft (for Minecraft), and I need 2 functions :

  • the cardinal function #foo (for a given table named "foo")
  • popping out the last element from a table, for example : foo[#foo] = nil (is there something better ?)

What are the respective complexity of these functions ? I particularly need a O(1) way to pop the last element of a table.

Sorry for bad english, thanks in advance.


Solution

  • ACcording to lua 5.4 reference (section 3.4.7), #t is logarithmic, not linear. It is explicitely written since lua 5.3, but probably it was also the case in lua 5.1 and 5.2 as well. It is also logarithmic in luajit.

    But aS already said in the other answer above, you need to manually keep a record of the length if you want it to be O(1).