File indexing completed on 2025-09-13 08:53:31
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/cont/Span.hh"
0012 #include "corecel/data/LdgIterator.hh"
0013 #include "corecel/math/Algorithms.hh"
0014
0015 namespace celeritas
0016 {
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template<class KeyQuantity, class ValueId>
0033 class GridIdFinder
0034 {
0035 static_assert(KeyQuantity::unit_type::value() > 0, "Invalid Quantity");
0036 static_assert(sizeof(typename ValueId::size_type), "Invalid OpaqueId");
0037
0038 public:
0039
0040
0041 using argument_type = KeyQuantity;
0042 using result_type = ValueId;
0043
0044 using SpanConstGrid = LdgSpan<typename KeyQuantity::value_type const>;
0045 using SpanConstValue = LdgSpan<result_type const>;
0046
0047
0048 public:
0049
0050 inline CELER_FUNCTION GridIdFinder(SpanConstGrid, SpanConstValue);
0051
0052
0053 inline CELER_FUNCTION result_type operator()(argument_type arg) const;
0054
0055 private:
0056 SpanConstGrid grid_;
0057 SpanConstValue value_;
0058 };
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 template<class K, class V>
0071 CELER_FUNCTION
0072 GridIdFinder<K, V>::GridIdFinder(SpanConstGrid grid, SpanConstValue value)
0073 : grid_(grid), value_(value)
0074 {
0075 CELER_EXPECT(grid_.size() == value_.size() + 1);
0076 }
0077
0078
0079
0080
0081
0082 template<class K, class V>
0083 CELER_FUNCTION auto
0084 GridIdFinder<K, V>::operator()(argument_type quant) const -> result_type
0085 {
0086 auto iter
0087 = celeritas::lower_bound(grid_.begin(), grid_.end(), quant.value());
0088 if (iter == grid_.end()
0089 || (iter == grid_.begin() && quant.value() != *iter))
0090 {
0091
0092 return {};
0093 }
0094 else if (iter + 1 == grid_.end() || quant.value() != *iter)
0095 {
0096
0097
0098 --iter;
0099 }
0100 return value_[iter - grid_.begin()];
0101 }
0102
0103
0104 }