File indexing completed on 2026-10-29 09:13:54
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <type_traits>
0010
0011 #include "corecel/Types.hh"
0012 #include "corecel/cont/Range.hh"
0013 #include "corecel/data/CollectionBuilder.hh"
0014
0015 #include "Types.hh"
0016
0017 namespace celeritas
0018 {
0019 class GeoParamsInterface;
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template<class T, class G, class F>
0033 inline Collection<T, Ownership::value, MemSpace::host, ImplVolumeId>
0034 build_volume_collection(G const& geo, F&& fill_value)
0035 {
0036 static_assert(std::is_base_of_v<GeoParamsInterface, G>,
0037 "G must be a geometry class");
0038 static_assert(std::is_convertible_v<std::invoke_result_t<F, VolumeId>, T>,
0039 "Incorrect return type for F");
0040
0041
0042 auto fill_or_default = [&](ImplVolumeId iv_id) -> T {
0043 if (auto vol_id = geo.volume_id(iv_id))
0044 {
0045 return fill_value(vol_id);
0046 }
0047 return {};
0048 };
0049
0050
0051 Collection<T, Ownership::value, MemSpace::host, ImplVolumeId> result;
0052 auto num_impl_volumes = geo.impl_volumes().size();
0053 resize(&result, num_impl_volumes);
0054
0055
0056 for (auto iv_id : range(ImplVolumeId{num_impl_volumes}))
0057 {
0058 result[iv_id] = fill_or_default(iv_id);
0059 }
0060 return result;
0061 }
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 template<class T>
0074 class VolumeMapFiller
0075 {
0076 static_assert(std::is_same_v<typename T::key_type, VolumeId>,
0077 "Not a map of volume to value");
0078
0079 public:
0080 using mapped_type = typename T::mapped_type;
0081
0082 public:
0083
0084 explicit VolumeMapFiller(T const& m) : from_vol_(m) {}
0085
0086
0087 mapped_type operator()(VolumeId vol_id) const
0088 {
0089 if (auto iter = from_vol_.find(vol_id); iter != from_vol_.end())
0090 {
0091 return iter->second;
0092 }
0093 return {};
0094 }
0095
0096 private:
0097 T const& from_vol_;
0098 };
0099
0100
0101 }