lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 20/08/16 09:01 PM, Martin wrote:
On 16-08-20 03:34 AM, Soni L. wrote:
Each C API function
should handle length on its own way.
Ok.

1. Will "table.insert(t, el)" and "t[#t+ 1] = el" remain equivalent?

No. The latter shall be a syntax error ("unexpected symbol near '#'").

2. Can you offer length algorithm that is O(1). Id est consumes constant
time to determine sequence length of table?

Let's break this question down:

"Can you offer length algorithm"

No.

"that is O(1). Id est consumes constant time to determine sequence length of table?"

Yes.

2.1 Because something like

local len = 1
while (t[len] ~= nil) do
   len = len + 1
end
return len

is O(n) and behaves like C's strlen().

You should probably return len - 1. Otherwise {} has len 1 and {1} has len 2.


I'm getting tired or assertions that Lua "should" something. It's not.


Tables shall be k/v pairs. The concept of an "array" shall be userland.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.