Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:46

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