File indexing completed on 2026-12-04 10:45:19
0001
0002
0003
0004
0005
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
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 template<class T>
0043 using RangeIter = detail::range_iter<T>;
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058 template<class T>
0059 class Range
0060 {
0061 using TraitsT = detail::RangeTypeTraits<T>;
0062
0063 public:
0064
0065
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
0076
0077
0078 constexpr Range() = default;
0079
0080
0081 explicit CELER_CEF Range(T end) : end_(const_iterator{end}) {}
0082
0083
0084 CELER_CEF Range(T begin, T end)
0085 : begin_(const_iterator{begin}), end_(const_iterator{end})
0086 {
0087 }
0088
0089
0090
0091
0092
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
0102 CELER_CEF value_type operator[](size_type i) const
0103 {
0104 return *(begin_ + i);
0105 }
0106
0107
0108 CELER_CEF size_type size() const
0109 {
0110 return TraitsT::to_counter(*end_) - TraitsT::to_counter(*begin_);
0111 }
0112
0113
0114 CELER_CEF bool empty() const { return begin_ == end_; }
0115
0116
0117 CELER_CEF value_type front() const { return *begin_; }
0118
0119
0120 CELER_CEF value_type back() const { return (*this)[this->size() - 1]; }
0121
0122
0123
0124
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
0137
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
0144
0145
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
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
0165
0166 template<class T>
0167 class Count
0168 {
0169 using TraitsT = detail::RangeTypeTraits<T>;
0170
0171 public:
0172
0173
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
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
0205
0206 template<class T>
0207 CELER_CEF Range<T> range(T end)
0208 {
0209 return Range{end};
0210 }
0211
0212
0213
0214
0215
0216 template<class T>
0217 CELER_CEF Count<T> count()
0218 {
0219 return Count<T>{};
0220 }
0221
0222
0223
0224
0225
0226 template<class T>
0227 CELER_CEF Count<T> count(T begin)
0228 {
0229 return Count{begin};
0230 }
0231
0232
0233 }