Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:24:52

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file orange/univ/detail/RaggedRightIndexer.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/Array.hh"
0012 #include "corecel/cont/Range.hh"
0013 #include "geocel/Types.hh"
0014 #include "orange/OrangeData.hh"
0015 
0016 namespace celeritas
0017 {
0018 namespace detail
0019 {
0020 //---------------------------------------------------------------------------//
0021 /*!
0022  * Index into flattened, ragged-right, 2D data, from index to coords.
0023  *
0024  * For example, consider three arrays of different sizes:
0025  *  A = [a1, a2]
0026  *  B = [b1, b2, b3]
0027  *  C = [c1]
0028  *
0029  *  Flattening them into a single array gives
0030  *
0031  *  flattened = [a1, a2, b1, b2, b3, c1]
0032  *
0033  *  Within this array, index of 4 (element b3) returns coords [1, 2].
0034  */
0035 template<size_type N>
0036 class RaggedRightIndexer
0037 {
0038   public:
0039     //!@{
0040     //! \name Type aliases
0041     using Coords = Size2;
0042     //!@}
0043 
0044   public:
0045     // Construct from RaggedRightIndexerData
0046     explicit inline CELER_FUNCTION
0047     RaggedRightIndexer(RaggedRightIndexerData<N> const& rrd);
0048 
0049     //// METHODS ////
0050 
0051     // Convert ragged indices to a flattened index
0052     inline CELER_FUNCTION size_type operator()(Coords coords) const;
0053 
0054   private:
0055     //// DATA ////
0056 
0057     RaggedRightIndexerData<N> const& rrd_;
0058 };
0059 
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Index into flattened, ragged-right, 2D data, from coords to index.
0063  *
0064  * For example, consider three arrays of different sizes:
0065  *  A = [a1, a2]
0066  *  B = [b1, b2, b3]
0067  *  C = [c1]
0068  *
0069  *  Flattening them into a single array gives
0070  *
0071  *  flattened = [a1, a2, b1, b2, b3, c1]
0072  *
0073  *  Within this array, coords [1, 2] (element b3) returns index 4.
0074  */
0075 template<size_type N>
0076 class RaggedRightInverseIndexer
0077 {
0078   public:
0079     //!@{
0080     //! \name Type aliases
0081     using Coords = Array<size_type, 2>;
0082     //!@}
0083 
0084   public:
0085     // Construct from RaggedRightIndexerData
0086     explicit inline CELER_FUNCTION
0087     RaggedRightInverseIndexer(RaggedRightIndexerData<N> const& rrd);
0088 
0089     // Convert a flattened index into ragged indices
0090     inline CELER_FUNCTION Coords operator()(size_type index) const;
0091 
0092   private:
0093     RaggedRightIndexerData<N> const& rrd_;
0094 };
0095 
0096 //---------------------------------------------------------------------------//
0097 // INLINE DEFINITIONS
0098 //---------------------------------------------------------------------------//
0099 /*!
0100  * Construct fom RaggedRightIndexerData.
0101  */
0102 template<size_type N>
0103 CELER_FUNCTION
0104 RaggedRightIndexer<N>::RaggedRightIndexer(RaggedRightIndexerData<N> const& rrd)
0105     : rrd_(rrd)
0106 {
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 /*!
0111  * Convert ragged indices to a flattened index.
0112  */
0113 template<size_type N>
0114 CELER_FUNCTION size_type RaggedRightIndexer<N>::operator()(Coords coords) const
0115 {
0116     auto const& offsets = rrd_.offsets;
0117     CELER_EXPECT(coords[0] < N);
0118     CELER_EXPECT(coords[1] < offsets[coords[0] + 1] - offsets[coords[0]]);
0119 
0120     return offsets[coords[0]] + coords[1];
0121 }
0122 
0123 //---------------------------------------------------------------------------//
0124 /*!
0125  * Construct fom RaggedRightIndexerData.
0126  */
0127 template<size_type N>
0128 CELER_FUNCTION RaggedRightInverseIndexer<N>::RaggedRightInverseIndexer(
0129     RaggedRightIndexerData<N> const& rrd)
0130     : rrd_(rrd)
0131 {
0132 }
0133 
0134 //---------------------------------------------------------------------------//
0135 /*!
0136  * Convert a flattened index into ragged indices.
0137  */
0138 template<size_type N>
0139 CELER_FUNCTION typename RaggedRightInverseIndexer<N>::Coords
0140 RaggedRightInverseIndexer<N>::operator()(size_type index) const
0141 {
0142     auto const& offsets = rrd_.offsets;
0143     CELER_EXPECT(index < offsets.back());
0144 
0145     size_type i = 0;
0146     while (index >= offsets[i + 1])
0147     {
0148         ++i;
0149     }
0150 
0151     return Coords{i, index - offsets[i]};
0152 }
0153 
0154 //---------------------------------------------------------------------------//
0155 }  // namespace detail
0156 }  // namespace celeritas