It is often useful to generate a table where item keys represent values that exist in the input data, and the item values represent the number of occurrences of the respective values. However, this involves quite a few operations per data set value, and there seems to be no particularly elegant or efficient solutions at this point.
The main problem from the scripting point of view is the "first occurrence" case, which needs to be handled specifically, as we can't increment a field that does not exist.
Example from a graphics library function that returns the most frequently seen pixel value within a square area:
local colors = {};
local step = 1 / scale;
size *= .5;
for local yy = y - size, y + size, step
for local xx = x - size, x + size, step
{
local c = getpixel_nearest(src, xx, yy, scx, scy,
m0, m1, scale, size);
if tryindex(colors, c)
colors[c] += 1;
else
colors[c] = 1;
}
By implementing this feature as a built-in native function, with access to table C interfaces and internals, we can take a few shortcuts, and drastically improve performance.
It is often useful to generate a table where item keys represent values that exist in the input data, and the item values represent the number of occurrences of the respective values. However, this involves quite a few operations per data set value, and there seems to be no particularly elegant or efficient solutions at this point.
The main problem from the scripting point of view is the "first occurrence" case, which needs to be handled specifically, as we can't increment a field that does not exist.
Example from a graphics library function that returns the most frequently seen pixel value within a square area:
By implementing this feature as a built-in native function, with access to table C interfaces and internals, we can take a few shortcuts, and drastically improve performance.