lua-users home
lua-l archive

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


On 01/11/2019 09.55, bil til wrote:
I assume quite often a very long listed/indexed table might have additionally some further key-value pairs with "hash-keys". E. g. a polyline/xy-curve with very many points might additionally have a name field and maybe some further info fields.

If I want to iterate over such "further info fields" in a fast way in
lua (and I do NOT know the key names, or I possibly would like to check such "further key names"), do I really have to iterate over the
complete table with pairs? (is this not a complete waste of time, as
I then possibly get also all the ipairs entries of the table for iteration in my for loop?).

If you want to iterate over different collections of fields, use
multiple tables and put the <LOTS of stuff that you only want SOMETIMES>
into a separate table.  Then you can iterate quickly over either the
list or the extra fields.  E.g.:

  curve = { { 23, 42 }, { 21, 37 }, metadata = { input = "mouse" } }

[ipairs gets you just the list, pairs on .metadata is the other fields]

or

  curve = { input = "mouse", data = { { 23, 42 }, { 21, 37 }, … }, … }

[tradeoff: different order, there's ONLY data in .data, but you'll have
to manually skip .data when doing pairs() on the outer container.]

or maybe even

  curve = { input = "mouse", x = { 23, 21, … }, y = { 42, 37, … }, … }

[split/"planar" representation tends to save A LOT of memory, if your
code can handle it]

(See also http://lua-users.org/lists/lua-l/2016-08/msg00256.html for
some more bad ideas.)

-- nobody