[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: fastest way to maintain nested tables of counters?
- From: Romulo <romuloab@...>
- Date: Sun, 15 May 2011 14:38:24 -0300
On Sun, May 15, 2011 at 12:51, Dirk Laurie <dpl@sun.ac.za> wrote:
> The following code reduces 6,7,8 to 4,5,6.
>
> function inc3(t, x, y, z)
> local tx = t[x]
> if not tx then tx={}; t[x] = tx end
> local txy = tx[y]
> if not txy then txy={}; tx[y] = txy end
> local txyz = txy[z];
> if not txyz then txy[z]=1 else txy[z]=txyz+1 end
> end
Another alternative (haven't benchmarked. sorry):
function inc3( t, x, y, z )
local a = t[ x ] if a == nil then t[ x ] = { [ y ] = { [ z ] = 1 }
}; return end
local b = a[ y ] if b == nil then a[ y ] = { [ z ] = 1 }; return end
local c = b[ z ] if c == nil then b[ z ] = 1 else b[ z ] = c + 1 end
end
--rb