File indexing completed on 2025-01-18 09:54:46
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include "detail/RangeImpl.hh"
0011
0012 namespace celeritas
0013 {
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 template<class T>
0042 using RangeIter = detail::range_iter<T>;
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 template<class T>
0058 class Range
0059 {
0060 using TraitsT = detail::RangeTypeTraits<T>;
0061
0062 public:
0063
0064
0065 using const_iterator = detail::range_iter<T>;
0066 using size_type = typename TraitsT::counter_type;
0067 using value_type = T;
0068
0069
0070 template<class U>
0071 using step_type = typename TraitsT::template common_type<U>;
0072
0073 public:
0074
0075
0076
0077 CELER_CONSTEXPR_FUNCTION Range() : begin_{}, end_{} {}
0078
0079
0080 CELER_CONSTEXPR_FUNCTION Range(T end) : begin_{}, end_(end) {}
0081
0082
0083 CELER_CONSTEXPR_FUNCTION Range(T begin, T end) : begin_(begin), end_(end)
0084 {
0085 }
0086
0087
0088
0089
0090
0091 CELER_CONSTEXPR_FUNCTION const_iterator begin() const { return begin_; }
0092 CELER_CONSTEXPR_FUNCTION const_iterator cbegin() const { return begin_; }
0093 CELER_CONSTEXPR_FUNCTION const_iterator end() const { return end_; }
0094 CELER_CONSTEXPR_FUNCTION const_iterator cend() const { return end_; }
0095
0096
0097
0098 CELER_CONSTEXPR_FUNCTION value_type operator[](size_type i) const
0099 {
0100 return *(begin_ + i);
0101 }
0102
0103
0104 CELER_CONSTEXPR_FUNCTION size_type size() const
0105 {
0106 return TraitsT::to_counter(*end_) - TraitsT::to_counter(*begin_);
0107 }
0108
0109
0110 CELER_CONSTEXPR_FUNCTION bool empty() const { return begin_ == end_; }
0111
0112
0113 CELER_CONSTEXPR_FUNCTION value_type front() const { return *begin_; }
0114
0115
0116 CELER_CONSTEXPR_FUNCTION value_type back() const
0117 {
0118 return (*this)[this->size() - 1];
0119 }
0120
0121
0122
0123
0124 template<class U, std::enable_if_t<std::is_signed<U>::value, bool> = true>
0125 CELER_CONSTEXPR_FUNCTION detail::StepRange<step_type<U>> step(U step)
0126 {
0127 if (step < 0)
0128 {
0129 return {TraitsT::increment(*end_, step), *begin_, step};
0130 }
0131
0132 return {*begin_, *end_, step};
0133 }
0134
0135
0136
0137 template<class U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
0138 CELER_CONSTEXPR_FUNCTION detail::StepRange<step_type<U>> step(U step)
0139 {
0140 return {*begin_, *end_, static_cast<size_type>(step)};
0141 }
0142
0143
0144 private:
0145 const_iterator begin_;
0146 const_iterator end_;
0147 };
0148
0149
0150
0151
0152
0153 template<class T>
0154 class Count
0155 {
0156 using TraitsT = detail::RangeTypeTraits<T>;
0157
0158 public:
0159
0160
0161 using const_iterator = detail::inf_range_iter<T>;
0162 using size_type = typename TraitsT::counter_type;
0163 using value_type = T;
0164
0165
0166 CELER_CONSTEXPR_FUNCTION Count() : begin_{} {}
0167 CELER_CONSTEXPR_FUNCTION Count(T begin) : begin_(begin) {}
0168
0169 CELER_CONSTEXPR_FUNCTION detail::InfStepRange<T> step(T step)
0170 {
0171 return {*begin_, step};
0172 }
0173
0174 CELER_CONSTEXPR_FUNCTION const_iterator begin() const { return begin_; }
0175 CELER_CONSTEXPR_FUNCTION const_iterator end() const
0176 {
0177 return const_iterator();
0178 }
0179 CELER_CONSTEXPR_FUNCTION bool empty() const { return false; }
0180
0181 private:
0182 const_iterator begin_;
0183 };
0184
0185
0186
0187
0188
0189 template<class T>
0190 CELER_CONSTEXPR_FUNCTION Range<T> range(T begin, T end)
0191 {
0192 return {begin, end};
0193 }
0194
0195
0196
0197
0198
0199 template<class T>
0200 CELER_CONSTEXPR_FUNCTION Range<T> range(T end)
0201 {
0202 return {end};
0203 }
0204
0205
0206
0207
0208
0209 template<class T>
0210 CELER_CONSTEXPR_FUNCTION Count<T> count()
0211 {
0212 return {};
0213 }
0214
0215
0216
0217
0218
0219 template<class T>
0220 CELER_CONSTEXPR_FUNCTION Count<T> count(T begin)
0221 {
0222 return {begin};
0223 }
0224
0225
0226 }