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