ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 755 B
JavaScript
var SearchUtils={bsearch:function(target,arr,comparator){if(void 0===arr||!arr||void 0===target)return-1;for(var mid,value,high=arr.length-1,low=0,cmp=comparator||SearchUtils.bsearch.numbers;low<=high;)if(0<(value=cmp(arr[mid=Math.floor((high+low)/2)],target)))high=mid-1;else{if(!(value<0))return mid;low=mid+1}return low}};SearchUtils.bsearch.numbers=function(element,target){return element-target},SearchUtils.bisectionSearch=function(target,low,high,precision,func){if("number"!=typeof target||"number"!=typeof low||"number"!=typeof high||"function"!=typeof func)return NaN;for(var value,mid=0,pre=0<precision?precision:1e-13;target<(value=func(mid=(high+low)/2))?high=mid:value<target&&(low=mid),pre<high-low;);return mid},module.exports=SearchUtils;