IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40176


Ignore:
Timestamp:
Oct 19, 2017, 2:04:55 PM (9 years ago)
Author:
eugene
Message:

add ohana_bisection_int

Location:
branches/eam_branches/ohana.20170822/src/libohana
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20170822/src/libohana/include/ohana.h

    r39457 r40176  
    485485/* in bisection.c */
    486486int ohana_bisection_double (double *values, int Nvalues, double threshold);
     487int ohana_bisection_int (int *values, int Nvalues, int threshold);
    487488
    488489unsigned int sprintf_float (char *output, float value);
  • branches/eam_branches/ohana.20170822/src/libohana/src/bisection.c

    r38459 r40176  
    3232  return (N);
    3333}
     34
     35// return the index of the last value < threshold
     36int ohana_bisection_int (int *values, int Nvalues, int threshold) {
     37
     38  int Nlo = 0;
     39  int Nhi = Nvalues - 1;
     40
     41  if (Nvalues < 1) return (-1);
     42  if (values[Nlo] > threshold) return (-1);
     43
     44  if (Nvalues < 2) return (0);
     45  if (values[Nhi] < threshold) return (Nhi);
     46
     47  int N;
     48  while (Nhi - Nlo > 4) {
     49    N = 0.5*(Nlo + Nhi);
     50    if (values[N] < threshold) {
     51      Nlo = MAX(N, 0);
     52    } else {
     53      Nhi = MIN(N + 1, Nvalues - 1);
     54    }
     55  }
     56  // values[Nlo] < threshold
     57  // values[Nhi] >= threshold
     58
     59  for (N = Nlo; N < Nhi; N++) {
     60    if (values[N] >= threshold) {
     61      return (N-1);
     62    }
     63  }
     64  return (N);
     65}
Note: See TracChangeset for help on using the changeset viewer.