Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:03

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 corecel/cont/Array.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cstddef>
0010 #include <utility>
0011 
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Fixed-size simple array for storage.
0020  *
0021  * The Array class is primarily used for point coordinates (e.g., \c Real3) but
0022  * is also used for other fixed-size data structures.
0023  *
0024  * This isn't fully standards-compliant with std::array: there's no support for
0025  * N=0 for example. Additionally it uses the native celeritas \c size_type,
0026  * even though this has *no* effect on generated code for values of N inside
0027  * the range of \c size_type.
0028  *
0029  * \note For supplementary functionality, include:
0030  * - \c corecel/math/ArrayUtils.hh for real-number vector/matrix applications
0031  * - \c corecel/math/ArrayOperators.hh for mathematical operators
0032  * - \c ArrayIO.hh for streaming and string conversion
0033  * - \c ArrayIO.json.hh for JSON input and output
0034  */
0035 template<class T, ::celeritas::size_type N>
0036 struct Array
0037 {
0038     //!@{
0039     //! \name Type aliases
0040     using value_type = T;
0041     using size_type = ::celeritas::size_type;
0042     using pointer = value_type*;
0043     using const_pointer = value_type const*;
0044     using reference = value_type&;
0045     using const_reference = value_type const&;
0046     using iterator = pointer;
0047     using const_iterator = const_pointer;
0048     //!@}
0049 
0050     //// DATA ////
0051 
0052     T data_[N];  //!< Storage
0053 
0054     //// ACCESSORS ////
0055 
0056     //!@{
0057     //! \name Element access
0058     CELER_CONSTEXPR_FUNCTION const_reference operator[](size_type i) const
0059     {
0060         return data_[i];
0061     }
0062     CELER_CONSTEXPR_FUNCTION reference operator[](size_type i)
0063     {
0064         return data_[i];
0065     }
0066     CELER_CONSTEXPR_FUNCTION const_reference front() const { return data_[0]; }
0067     CELER_CONSTEXPR_FUNCTION reference front() { return data_[0]; }
0068     CELER_CONSTEXPR_FUNCTION const_reference back() const
0069     {
0070         return data_[N - 1];
0071     }
0072     CELER_CONSTEXPR_FUNCTION reference back() { return data_[N - 1]; }
0073     CELER_CONSTEXPR_FUNCTION const_pointer data() const { return data_; }
0074     CELER_CONSTEXPR_FUNCTION pointer data() { return data_; }
0075 
0076     //! Access for structured unpacking
0077     template<std::size_t I>
0078     CELER_CONSTEXPR_FUNCTION T& get()
0079     {
0080         static_assert(I < static_cast<std::size_t>(N));
0081         return data_[I];
0082     }
0083 
0084     //! Access for structured unpacking
0085     template<std::size_t I>
0086     CELER_CONSTEXPR_FUNCTION T const& get() const
0087     {
0088         static_assert(I < static_cast<std::size_t>(N));
0089         return data_[I];
0090     }
0091     //!@}
0092 
0093     //!@{
0094     //! \name Iterators
0095     CELER_CONSTEXPR_FUNCTION iterator begin() { return data_; }
0096     CELER_CONSTEXPR_FUNCTION iterator end() { return data_ + N; }
0097     CELER_CONSTEXPR_FUNCTION const_iterator begin() const { return data_; }
0098     CELER_CONSTEXPR_FUNCTION const_iterator end() const { return data_ + N; }
0099     CELER_CONSTEXPR_FUNCTION const_iterator cbegin() const { return data_; }
0100     CELER_CONSTEXPR_FUNCTION const_iterator cend() const { return data_ + N; }
0101     //!@}
0102 
0103     //!@{
0104     //! \name Capacity
0105     CELER_CONSTEXPR_FUNCTION bool empty() const { return N == 0; }
0106     static CELER_CONSTEXPR_FUNCTION size_type size() { return N; }
0107     //!@}
0108 
0109     //!@{
0110     //! \name  Operations
0111 
0112     //! Fill the array with a constant value
0113     CELER_CONSTEXPR_FUNCTION void fill(const_reference value)
0114     {
0115         for (size_type i = 0; i != N; ++i)
0116             data_[i] = value;
0117     }
0118     //!@}
0119 };
0120 
0121 //---------------------------------------------------------------------------//
0122 // INLINE DEFINITIONS
0123 //---------------------------------------------------------------------------//
0124 /*!
0125  * Test equality of two arrays.
0126  */
0127 template<class T, size_type N>
0128 CELER_CONSTEXPR_FUNCTION bool
0129 operator==(Array<T, N> const& lhs, Array<T, N> const& rhs)
0130 {
0131     for (size_type i = 0; i != N; ++i)
0132     {
0133         if (lhs[i] != rhs[i])
0134             return false;
0135     }
0136     return true;
0137 }
0138 
0139 //---------------------------------------------------------------------------//
0140 /*!
0141  * Test inequality of two arrays.
0142  */
0143 template<class T, size_type N>
0144 CELER_CONSTEXPR_FUNCTION bool
0145 operator!=(Array<T, N> const& lhs, Array<T, N> const& rhs)
0146 {
0147     return !(lhs == rhs);
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 }  // namespace celeritas
0152 
0153 //---------------------------------------------------------------------------//
0154 //! \cond
0155 namespace std
0156 {
0157 //---------------------------------------------------------------------------//
0158 //! Support structured binding: array size
0159 template<class T, celeritas::size_type N>
0160 struct tuple_size<celeritas::Array<T, N>>
0161 {
0162     static constexpr std::size_t value = N;
0163 };
0164 
0165 //! Support structured binding: array element type
0166 template<std::size_t I, class T, celeritas::size_type N>
0167 struct tuple_element<I, celeritas::Array<T, N>>
0168 {
0169     static_assert(I < std::tuple_size<celeritas::Array<T, N>>::value);
0170     using type = T;
0171 };
0172 
0173 //---------------------------------------------------------------------------//
0174 }  // namespace std
0175 //! \endcond