lua-users home
lua-l archive

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


On 3 June 2010 14:03, Matthew Wild <mwild1@gmail.com> wrote:
> On 1 June 2010 15:33, zuric <zurrix@gmail.com> wrote:
>> thanks very much.
>> i did some test with your file, but i have a problem with that.
>> when i getSize() of a table, the total size of the table will not include
>> the refrence object.
>> for example,
>> test = {1,2}
>> test1 = {1,2,3,4,5, link = test}
>> the size of test1 will not include test
>> any idea ?
>>
>
> Yes, that's intentional. Getting the "size" of a table including its
> descendents isn't trivial sometimes, but if you have a simple tree
> then it isn't hard. Just loop over all the elements and add up the
> sizes, something like:
>
> local function get_total_size(t)
>    local size = debug.getsize(t);
>    if type(t) == "table" then
>        for k, v in pairs(t) do
>            size = size + get_total_size(t);
>        end
>    end
>    return size;
> end
>

Why I never post code to mailing lists :)

I of course meant something more like:

local function get_total_size(t)
   local size = debug.getsize(t);
   if type(t) == "table" then
       for k, v in pairs(t) do
           size = size + get_total_size(k) + get_total_size(v);
       end
   end
   return size;
end

Matthew