vl_binsearch.c
Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00009 All rights reserved.
00010 
00011 This file is part of the VLFeat library and is made available under
00012 the terms of the BSD license (see the COPYING file).
00013 */
00014 
00015 #include <mexutils.h>
00016 
00017 void
00018 mexFunction(int nout, mxArray *out[],
00019             int nin, const mxArray *in[])
00020 {
00021   enum { IN_B=0, IN_X, IN_END } ;
00022   enum { OUT_IDX=0 } ;
00023   vl_size numElementsX, numElementsB ;
00024   const double *X, *B ;
00025   double *IDX ;
00026 
00027   if (nout > 1) {
00028     vlmxError(vlmxErrTooManyOutputArguments, NULL) ;
00029   }
00030   if (nin != 2) {
00031     vlmxError(vlmxErrInvalidArgument,
00032               "Incorrect number of arguments.") ;
00033   }
00034   if (! vlmxIsPlain (IN(B)) ||
00035       ! vlmxIsPlain (IN(X)) ) {
00036     vlmxError(vlmxErrInvalidArgument,
00037               "All arguments must be plain arrays.") ;
00038   }
00039 
00040   numElementsX = mxGetNumberOfElements(IN(X)) ;
00041   numElementsB = mxGetNumberOfElements(IN(B)) ;
00042 
00043   {
00044     mwSize const * dimensions = mxGetDimensions(IN(X)) ;
00045     vl_size numDimensions = mxGetNumberOfDimensions(IN(X)) ;
00046     OUT(IDX) = mxCreateNumericArray (numDimensions, dimensions, mxDOUBLE_CLASS, mxREAL) ;
00047   }
00048 
00049   /* if B is empty it defines only [-inf, +inf) */
00050   if (numElementsB == 0) return ;
00051 
00052   IDX = mxGetPr(OUT(IDX)) ;
00053   X = mxGetPr(IN(X)) ;
00054   B = mxGetPr(IN(B)) ;
00055 
00056   {
00057     vl_uindex i ;
00058     for (i = 0 ; i < numElementsX ; ++i) {
00059       double x = X[i] ;
00060       vl_uindex blower = 0 ;
00061       vl_uindex bupper = numElementsB - 1 ;
00062       vl_uindex bsplit ;
00063 
00064       if (x < B[0]) {
00065         IDX [i] = 0 ;
00066         continue ;
00067       }
00068 
00069       if (x >= B[numElementsB - 1]) {
00070         IDX [i] = numElementsB ;
00071         continue ;
00072       }
00073 
00074       while (blower + 1 < bupper) {
00075         bsplit = (bupper + blower) / 2 ;
00076         if (x < B[bsplit]) bupper = bsplit ;
00077         else blower = bsplit ;
00078       }
00079       IDX [i] = blower + 1 ;
00080     }
00081   }
00082 }


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51