internal int best(float[] a, int step)
{
EncodeAuxNearestMatch nt=c.nearest_tree;
EncodeAuxThreshMatch tt=c.thresh_tree;
int ptr=0;
// we assume for now that a thresh tree is the only other possibility
if(tt!=null)
{
int index=0;
// find the quant val of each scalar
for(int k=0,o=step*(dim-1);k<dim;k++,o-=step)
{
int i;
// linear search the quant list for now; it's small and although
// with > 8 entries, it would be faster to bisect, this would be
// a misplaced optimization for now
for(i=0;i<tt.threshvals-1;i++)
{
if(a[o]<tt.quantthresh[i])
{
break;
}
}
index=(index*tt.quantvals)+tt.quantmap[i];
}
// regular lattices are easy :-)
if(c.lengthlist[index]>0)
{
// is this unused? If so, we'll
// use a decision tree after all
// and fall through
return(index);
}
}
if(nt!=null)
{
// optimized using the decision tree
while(true)
{
double cc=0.0;
int p=nt.p[ptr];
int q=nt.q[ptr];
for(int k=0,o=0;k<dim;k++,o+=step)
{
cc+=(valuelist[p+k]-valuelist[q+k])*
(a[o]-(valuelist[p+k]+valuelist[q+k])*.5);
}
if(cc>0.0)
{ // in A
ptr= -nt.ptr0[ptr];
}
else
{ // in B
ptr= -nt.ptr1[ptr];
}
if(ptr<=0)break;
}
return(-ptr);
}
// brute force it!
{
int besti=-1;
float best=0.0f;
int e=0;
for(int i=0;i<entries;i++)
{
if(c.lengthlist[i]>0)
{
float _this=dist(dim, valuelist, e, a, step);
if(besti==-1 || _this<best)
{
best=_this;
besti=i;
}
}
e+=dim;
}
return(besti);
}
}