lua-users home
lua-l archive

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

On 18-Jan-06, at 10:48 AM, Asko Kauppi wrote:

Good list, Mike! :)

Some comments/questions, not necessarily for you, but in general..

1. Is #t NOT expected to work, if a table has both integer (1..N) and other (string, etc) keys? If so, it's really too easy to mess it up, I hope #t would always find the 'N', no matter which other keys there are (note: hole issue is different).

I would have thought so, too.

On the other hand, maxn works with sparse arrays only if they're not "sparse at the end", as it were. I personally think this is simply a bug waiting to get a user of maxn, and that people who actually use sparse arrays should come up with their own protocol to record the "size" of the array. I've gone back to using a 'n' field, myself:

  function sparse(...)
    return {n = select('#', ...), ...}

  function sparseadd(t, ...)
    local count = select('#', ...)
    local n = t.n
    for i = 1, count do t[n+i] = select(i, ...) end
    t.n = n + count
    return t

  function sparsenext(t, i)
    for j = i + 1, t.n do
      if t[j] ~= nil then return j, t[j] end

  function sparsepairs(t)
    return sparsenext, t, 0

  t = sparse(2, nil, nil, 4, nil)
  sparseadd(t, nil, 3, 7)
  for i, val in sparsepairs(t) do
    print(i, val)

  1       2
  4       4
  7       3
  8       7

Q: What would happen if sparse* were implemented with maxn?