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/EnumArray.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <type_traits>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 
0015 namespace celeritas
0016 {
0017 #define CFIF_ CELER_FORCEINLINE_FUNCTION
0018 
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Thin wrapper for an array of enums for accessing by enum instead of int.
0022  *
0023  * The enum *must* be a zero-indexed contiguous enumeration with a \c size_
0024  * enumeration as its last value.
0025  *
0026  * \todo The template parameters are reversed!!!
0027  */
0028 template<class E, class T>
0029 struct EnumArray
0030 {
0031     static_assert(std::is_enum<E>::value, "Template parameter must be an enum");
0032 
0033     //!@{
0034     //! \name Type aliases
0035     using key_type = E;
0036     using value_type = T;
0037     using size_type = std::underlying_type_t<E>;
0038     using pointer = value_type*;
0039     using const_pointer = value_type const*;
0040     using reference = value_type&;
0041     using const_reference = value_type const&;
0042     using iterator = pointer;
0043     using const_iterator = const_pointer;
0044     //!@}
0045 
0046     enum
0047     {
0048         N = static_cast<size_type>(key_type::size_)
0049     };
0050 
0051     //// DATA ////
0052 
0053     T data_[N];  //!< Storage
0054 
0055     //! Get an element
0056     CFIF_ reference operator[](key_type const& k)
0057     {
0058         return data_[static_cast<size_type>(k)];
0059     }
0060 
0061     //! Get an element (const)
0062     CFIF_ const_reference operator[](key_type const& k) const
0063     {
0064         return data_[static_cast<size_type>(k)];
0065     }
0066 
0067     //!@{
0068     //! Element access
0069     CFIF_ const_reference front() const { return data_[0]; }
0070     CFIF_ reference front() { return data_[0]; }
0071     CFIF_ const_reference back() const { return data_[N - 1]; }
0072     CFIF_ reference back() { return data_[N - 1]; }
0073     CFIF_ const_pointer data() const { return data_; }
0074     CFIF_ pointer data() { return data_; }
0075     //!@}
0076 
0077     //!@{
0078     //! Iterator access
0079     CFIF_ iterator begin() { return data_; }
0080     CFIF_ iterator end() { return data_ + N; }
0081     CFIF_ const_iterator begin() const { return data_; }
0082     CFIF_ const_iterator end() const { return data_ + N; }
0083     CFIF_ const_iterator cbegin() const { return data_; }
0084     CFIF_ const_iterator cend() const { return data_ + N; }
0085     //!@}
0086 
0087     //!@{
0088     //! Capacity
0089     CELER_CONSTEXPR_FUNCTION bool empty() const { return N == 0; }
0090     CELER_CONSTEXPR_FUNCTION size_type size() const { return N; }
0091     //!@}
0092 };
0093 
0094 #undef CFIF_
0095 
0096 //---------------------------------------------------------------------------//
0097 }  // namespace celeritas