Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:55

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/detail/TransformRecordInserter.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Macros.hh"
0011 #include "corecel/data/Collection.hh"
0012 #include "corecel/data/CollectionBuilder.hh"
0013 #include "corecel/data/DedupeCollectionBuilder.hh"
0014 
0015 #include "../OrangeData.hh"
0016 #include "../transform/VariantTransform.hh"
0017 
0018 namespace celeritas
0019 {
0020 namespace detail
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Construct a compressed transform from a variant.
0025  *
0026  * TODO: deduplicate transforms via hashes? Define special compressed transform
0027  * for 90-degree rotations?
0028  */
0029 class TransformRecordInserter
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     template<class T>
0035     using Items = Collection<T, Ownership::value, MemSpace::host>;
0036     //!@}
0037 
0038   public:
0039     // Construct with pointers to target data
0040     inline TransformRecordInserter(Items<TransformRecord>* transforms,
0041                                    Items<real_type>* reals);
0042 
0043     // Return a transform ID from a transform variant
0044     inline TransformId operator()(VariantTransform const& tr);
0045 
0046     // Construct a transform using known type
0047     template<class T>
0048     inline TransformId operator()(T const& tr);
0049 
0050   private:
0051     TransformId null_transform_;
0052     CollectionBuilder<TransformRecord> transforms_;
0053     DedupeCollectionBuilder<real_type> reals_;
0054 };
0055 
0056 //---------------------------------------------------------------------------//
0057 // INLINE DEFINITIONS
0058 //---------------------------------------------------------------------------//
0059 /*!
0060  * Construct with pointers to target data.
0061  */
0062 TransformRecordInserter::TransformRecordInserter(
0063     Items<TransformRecord>* transforms, Items<real_type>* reals)
0064     : transforms_{transforms}, reals_{reals}
0065 {
0066     CELER_EXPECT(transforms && reals);
0067 }
0068 
0069 //---------------------------------------------------------------------------//
0070 /*!
0071  * Construct from a transform variant.
0072  */
0073 TransformId TransformRecordInserter::operator()(VariantTransform const& tr)
0074 {
0075     CELER_ASSUME(!tr.valueless_by_exception());
0076     return std::visit(*this, tr);
0077 }
0078 
0079 //---------------------------------------------------------------------------//
0080 /*!
0081  * Construct from a transform with a known type.
0082  */
0083 template<class T>
0084 TransformId TransformRecordInserter::operator()(T const& tr)
0085 {
0086     /*!
0087      * \todo Add equality and hash for TransformRecord and replace this with
0088      * just a dedupe collection builder.
0089      */
0090     if constexpr (std::is_same_v<T, NoTransformation>)
0091     {
0092         // Reuse the same null transform ID everywhere
0093         if (null_transform_)
0094         {
0095             return null_transform_;
0096         }
0097         // Save the transform ID for later
0098         null_transform_ = transforms_.size_id();
0099     }
0100 
0101     TransformRecord record;
0102     record.type = tr.transform_type();
0103     auto data = tr.data();
0104     record.data_offset = *reals_.insert_back(data.begin(), data.end()).begin();
0105 
0106     CELER_ASSERT(record);
0107     return transforms_.push_back(record);
0108 }
0109 
0110 //---------------------------------------------------------------------------//
0111 }  // namespace detail
0112 }  // namespace celeritas