XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
binary_search.hpp
Go to the documentation of this file.
1 #ifndef BINARY_SEARCH_HPP
2 #define BINARY_SEARCH_HPP
3 
4 #include "space_settings.hpp"
5 
6 // Binary search. Returns closest index of x0 that is less than x, i.e. i such that x0(i) <= x < x0(i+1)
7 // Returns -1 if x<x0(0) or x>=x0(n-1)
8 template<class Device>
9 KOKKOS_INLINE_FUNCTION int binary_search(const View<double*,CLayout,Device>& x0, const double x){
10  const int& n = x0.extent(0);
11  int low = 0;
12  int high = n - 1;
13  int mid;
14  while (low <= high) {
15  mid = low + (high - low) / 2;
16  if (x0(mid) <= x) {
17  if (mid == n - 1 || x0(mid + 1) > x) {
18  return mid; // Found the largest value <= xnew_i
19  }
20  low = mid + 1;
21  } else {
22  high = mid - 1;
23  }
24  }
25  return -1; // In case all elements in x0 are greater than xnew_i
26 }
27 
28 #endif
KOKKOS_INLINE_FUNCTION int binary_search(const View< double *, CLayout, Device > &x0, const double x)
Definition: binary_search.hpp:9