Q about Coll Index (interpolation index)

djlbe's icon

I've search a long time but I can't find the solution. I'm searching for a kind of object coll. where, when I send a number of index which doesn't exist the object send the value of the nearest index.

so if I've

[1, 64]
[3, 65]
[7, 66]
[9, 97]
[20, 88]

if I send 8 it will find the index 7 and return 66.

I precise my index are really large value because they report the position in a file. and I need to connect a phazor with a snapshot to this object. it is possible to find a solution ?

thanks in advance

Inear's icon

Hi, you could try with a funbuff.

dhjdhjdhj's icon

I do this kind of thing with JavaScript objects now.

jamesson's icon

coll has prev and next, no?

djlbe's icon

Thx lnear that's the best solution.

But how did u do that in Javascript dhjdhjdhj ? have u an exemple ?

dhjdhjdhj's icon

Well, I've only been hacking JavaScript for a week or so, but here's an example that works in node.js and would probably work in Max

function w(s)
{
//post(s); post();
    console.log(s);
}

var obj = {};
obj[1] = 64;
obj[3] = 65;
obj[7] = 66;
obj[9] = 97;
obj[20] = 88;

obj[9999999] = -1; // Fencepost --- make it easy

// get the indexes and sort them
var indexes = [];
for (var i in obj)
{
indexes.push(i);
}

indexes.sort(
function(a,b)
{
return a-b;
}
);

function NearestIndex(arr, index)
{
var left = 0;
var right = arr.length - 1;
var mid = 0;
while (left
{
mid = parseInt( (left + right) / 2);
if (index > arr[mid])
//then
left = mid + 1;
else if (index < arr[mid])
//then
right = mid - 1;
else break;

}

if (index != arr[mid])
//then
mid--;
return mid;
}

var n = NearestIndex(indexes, 1);
w(n);

n = NearestIndex(indexes,9);
w(n);

n = NearestIndex(indexes, 7);
w(n);

n = NearestIndex(indexes, 24);
w(n);

yp's icon

you can also do pretty slick interpolation of values with pattr storage (see ex. in the help file)