2018-08-08 07:00:48 -05:00
|
|
|
//! \file search.h
|
|
|
|
|
//! Search algorithms
|
|
|
|
|
|
2018-07-09 09:27:34 -05:00
|
|
|
#ifndef OPENMC_SEARCH_H
|
|
|
|
|
#define OPENMC_SEARCH_H
|
|
|
|
|
|
2018-10-11 12:55:33 -05:00
|
|
|
#include <algorithm> // for lower_bound, upper_bound
|
2018-07-09 09:27:34 -05:00
|
|
|
|
|
|
|
|
namespace openmc {
|
|
|
|
|
|
2018-08-08 07:00:48 -05:00
|
|
|
//! Perform binary search
|
|
|
|
|
|
2018-07-09 09:27:34 -05:00
|
|
|
template<class It, class T>
|
|
|
|
|
typename std::iterator_traits<It>::difference_type lower_bound_index(
|
|
|
|
|
It first, It last, const T& value)
|
|
|
|
|
{
|
2018-10-05 22:49:29 -04:00
|
|
|
if (*first == value)
|
|
|
|
|
return 0;
|
2022-01-06 14:29:23 +01:00
|
|
|
return std::lower_bound(first, last, value) - first - 1;
|
2018-07-09 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-11 12:55:33 -05:00
|
|
|
template<class It, class T>
|
|
|
|
|
typename std::iterator_traits<It>::difference_type upper_bound_index(
|
|
|
|
|
It first, It last, const T& value)
|
|
|
|
|
{
|
2022-01-06 14:29:23 +01:00
|
|
|
return std::upper_bound(first, last, value) - first - 1;
|
2018-10-11 12:55:33 -05:00
|
|
|
}
|
|
|
|
|
|
2018-08-08 07:00:48 -05:00
|
|
|
} // namespace openmc
|
2018-07-09 09:27:34 -05:00
|
|
|
|
2018-08-08 07:00:48 -05:00
|
|
|
#endif // OPENMC_SEARCH_H
|