Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 10:45:19

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/Range.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/data/Ldg.hh"
0010 
0011 #include "detail/RangeImpl.hh"
0012 
0013 namespace celeritas
0014 {
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * \fn range
0018  * \tparam T Value type to iterate over
0019  * Get iterators over a range of values, or a semi-infinite range.
0020  *
0021  * \par Code Sample:
0022  * \code
0023 
0024     for (auto i : Range(1, 5))
0025         cout << i << "\n";
0026 
0027     // Range of [0, 10)
0028     for (auto u : Range(10u))
0029         cout << u << "\n";
0030 
0031     for (auto c : Range('a', 'd'))
0032         cout << c << "\n";
0033 
0034     for (auto i : Count(100).step(-3))
0035         if (i < 90) break;
0036         else        cout << i << "\n";
0037 
0038  * \endcode
0039  */
0040 
0041 //! Publicly expose traits for countable (OpaqueId, enum, init) type T
0042 template<class T>
0043 using RangeIter = detail::range_iter<T>;
0044 
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * Proxy container for iterating over a range of integral values.
0048  *
0049  * Here, T can be any of:
0050  * - an integer,
0051  * - an enum that has contiguous zero-indexed values and a "size_" enumeration
0052  *   value indicating how many, or
0053  * - an OpaqueId.
0054  *
0055  * It is OK to dereference the end iterator! The result should just be the
0056  * off-the-end value for the range, e.g. `FooEnum::size_` or `bar.size()`.
0057  */
0058 template<class T>
0059 class Range
0060 {
0061     using TraitsT = detail::RangeTypeTraits<T>;
0062 
0063   public:
0064     //!@{
0065     //! \name Type aliases
0066     using const_iterator = detail::range_iter<T>;
0067     using size_type = typename TraitsT::counter_type;
0068     using value_type = T;
0069     //@}
0070 
0071     template<class U>
0072     using step_type = typename TraitsT::template common_type<U>;
0073 
0074   public:
0075     //// CONSTRUCTORS ////
0076 
0077     //! Empty constructor for empty range
0078     constexpr Range() = default;
0079 
0080     //! Construct from stop
0081     explicit CELER_CEF Range(T end) : end_(const_iterator{end}) {}
0082 
0083     //! Construct from start/stop
0084     CELER_CEF Range(T begin, T end)
0085         : begin_(const_iterator{begin}), end_(const_iterator{end})
0086     {
0087     }
0088 
0089     //// CONTAINER-LIKE ACCESS ////
0090 
0091     //!@{
0092     //! Iterators
0093     CELER_CEF const_iterator const& begin() const& { return begin_; }
0094     CELER_CEF const_iterator const& end() const& { return end_; }
0095     CELER_CEF const_iterator begin() const&& { return begin_; }
0096     CELER_CEF const_iterator end() const&& { return end_; }
0097     CELER_CEF const_iterator cbegin() const { return begin_; }
0098     CELER_CEF const_iterator cend() const { return end_; }
0099     //!@}
0100 
0101     //! Array-like access
0102     CELER_CEF value_type operator[](size_type i) const
0103     {
0104         return *(begin_ + i);
0105     }
0106 
0107     //! Number of elements
0108     CELER_CEF size_type size() const
0109     {
0110         return TraitsT::to_counter(*end_) - TraitsT::to_counter(*begin_);
0111     }
0112 
0113     //! Whether the range has no elements
0114     CELER_CEF bool empty() const { return begin_ == end_; }
0115 
0116     //! First item in the range
0117     CELER_CEF value_type front() const { return *begin_; }
0118 
0119     //! Last item in the range
0120     CELER_CEF value_type back() const { return (*this)[this->size() - 1]; }
0121 
0122     //// STRIDED ACCESS ////
0123 
0124     //! Return a stepped range using a *signed* integer type
0125     template<class U, std::enable_if_t<std::is_signed<U>::value, bool> = true>
0126     CELER_CEF detail::StepRange<step_type<U>> step(U step)
0127     {
0128         if (step < 0)
0129         {
0130             return {TraitsT::increment(*end_, step), *begin_, step};
0131         }
0132 
0133         return {*begin_, *end_, step};
0134     }
0135 
0136     //! \cond
0137     //! Return a stepped range using an *unsigned* integer type
0138     template<class U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
0139     CELER_CEF detail::StepRange<step_type<U>> step(U step)
0140     {
0141         return {*begin_, *end_, static_cast<size_type>(step)};
0142     }
0143     //! \endcond
0144 
0145     //! Allow loading via ldg
0146     CELER_CEF friend Range ldg(Range const* r) noexcept
0147     {
0148         return Range{ldg(&r->begin_), ldg(&r->end_)};
0149     }
0150 
0151   private:
0152     //! Construct from iterators
0153     CELER_CEF Range(const_iterator begin, const_iterator end)
0154         : begin_(begin), end_(end)
0155     {
0156     }
0157 
0158     const_iterator begin_;
0159     const_iterator end_;
0160 };
0161 
0162 //---------------------------------------------------------------------------//
0163 /*!
0164  * Proxy container for an unbounded range with a given start value.
0165  */
0166 template<class T>
0167 class Count
0168 {
0169     using TraitsT = detail::RangeTypeTraits<T>;
0170 
0171   public:
0172     //!@{
0173     //! \name Type aliases
0174     using const_iterator = detail::inf_range_iter<T>;
0175     using size_type = typename TraitsT::counter_type;
0176     using value_type = T;
0177     //@}
0178 
0179     constexpr Count() = default;
0180     explicit CELER_CEF Count(T begin) : begin_(const_iterator{begin}) {}
0181 
0182     CELER_CEF detail::InfStepRange<T> step(T step) { return {*begin_, step}; }
0183 
0184     CELER_CEF const_iterator begin() const { return begin_; }
0185     CELER_CEF const_iterator end() const { return const_iterator(); }
0186     CELER_CEF bool empty() const { return false; }
0187 
0188   private:
0189     const_iterator begin_;
0190 };
0191 
0192 //---------------------------------------------------------------------------//
0193 /*!
0194  * Return a range over fixed beginning and end values.
0195  */
0196 template<class T>
0197 CELER_CEF Range<T> range(T begin, T end)
0198 {
0199     return Range{begin, end};
0200 }
0201 
0202 //---------------------------------------------------------------------------//
0203 /*!
0204  * Return a range with the default start value (0 for numeric types)
0205  */
0206 template<class T>
0207 CELER_CEF Range<T> range(T end)
0208 {
0209     return Range{end};
0210 }
0211 
0212 //---------------------------------------------------------------------------//
0213 /*!
0214  * Count upward from zero.
0215  */
0216 template<class T>
0217 CELER_CEF Count<T> count()
0218 {
0219     return Count<T>{};
0220 }
0221 
0222 //---------------------------------------------------------------------------//
0223 /*!
0224  * Count upward from a value.
0225  */
0226 template<class T>
0227 CELER_CEF Count<T> count(T begin)
0228 {
0229     return Count{begin};
0230 }
0231 
0232 //---------------------------------------------------------------------------//
0233 }  // namespace celeritas