Back to home page

EIC code displayed by LXR

 
 

    


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

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