consolidate binary() member functions of Comm and Balance into utils::binary_search()

This commit is contained in:
Axel Kohlmeyer
2021-09-06 16:58:14 -04:00
parent 801cd647c3
commit 898f8086db
7 changed files with 71 additions and 80 deletions

View File

@ -1284,6 +1284,28 @@ int utils::date2num(const std::string &date)
return num;
}
/* binary search in vector of ascending doubles */
int utils::binary_search(const double needle, const int n, const double *haystack)
{
int lo = 0;
int hi = n-1;
if (needle < haystack[lo]) return lo;
if (needle >= haystack[hi]) return hi;
// insure haystack[lo] <= needle < haystack[hi] at every iteration
// done when lo,hi are adjacent
int index = (lo+hi)/2;
while (lo < hi-1) {
if (needle < haystack[index]) hi = index;
else if (needle >= haystack[index]) lo = index;
index = (lo+hi)/2;
}
return index;
}
/* ----------------------------------------------------------------------
* Merge sort part 1: Loop over sublists doubling in size with each iteration.
* Pre-sort small sublists with insertion sort for better overall performance.