@@ -58,25 +58,25 @@ from other valid indices. Therefore, users should not expect the same checks are
58
58
to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
59
59
"""
60
60
struct OptionallyStaticUnitRange{T,F,L} <: AbstractUnitRange{T}
61
- start:: F
62
- stop:: L
63
-
64
- function OptionallyStaticUnitRange {T} (start, stop) where {T<: Real }
65
- if _get (start) isa T
66
- if _get (stop) isa T
67
- return new {T,typeof(start),typeof(stop)} (start, stop)
68
- else
69
- return OptionallyStaticUnitRange {T} (start, _convert (T, stop))
70
- end
71
- else
72
- return OptionallyStaticUnitRange {T} (_convert (T, start), stop)
73
- end
61
+ start:: F
62
+ stop:: L
63
+
64
+ function OptionallyStaticUnitRange {T} (start, stop) where {T<: Real }
65
+ if _get (start) isa T
66
+ if _get (stop) isa T
67
+ return new {T,typeof(start),typeof(stop)} (start, stop)
68
+ else
69
+ return OptionallyStaticUnitRange {T} (start, _convert (T, stop))
70
+ end
71
+ else
72
+ return OptionallyStaticUnitRange {T} (_convert (T, start), stop)
74
73
end
74
+ end
75
75
76
- function OptionallyStaticUnitRange (start, stop)
77
- T = promote_type (typeof (_get (start)), typeof (_get (stop)))
78
- return OptionallyStaticUnitRange {T} (start, stop)
79
- end
76
+ function OptionallyStaticUnitRange (start, stop)
77
+ T = promote_type (typeof (_get (start)), typeof (_get (stop)))
78
+ return OptionallyStaticUnitRange {T} (start, stop)
79
+ end
80
80
end
81
81
82
82
Base. first (r:: OptionallyStaticUnitRange{<:Any,Val{F}} ) where {F} = F
@@ -92,11 +92,11 @@ known_step(::Type{<:OptionallyStaticUnitRange{T}}) where {T} = one(T)
92
92
known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,<:Any,Val{L}}} ) where {L} = L
93
93
94
94
function Base. isempty (r:: OptionallyStaticUnitRange )
95
- if known_first (r) === oneunit (eltype (r))
96
- return unsafe_isempty_one_to (last (r))
97
- else
98
- return unsafe_isempty_unit_range (first (r), last (r))
99
- end
95
+ if known_first (r) === oneunit (eltype (r))
96
+ return unsafe_isempty_one_to (last (r))
97
+ else
98
+ return unsafe_isempty_unit_range (first (r), last (r))
99
+ end
100
100
end
101
101
102
102
unsafe_isempty_one_to (lst) = lst <= zero (lst)
@@ -108,26 +108,26 @@ unsafe_length_one_to(lst::T) where {T<:Int} = T(lst)
108
108
unsafe_length_one_to (lst:: T ) where {T} = Integer (lst - zero (lst))
109
109
110
110
Base. @propagate_inbounds function Base. getindex (r:: OptionallyStaticUnitRange , i:: Integer )
111
- if known_first (r) === oneunit (r)
112
- return get_index_one_to (r, i)
113
- else
114
- return get_index_unit_range (r, i)
115
- end
111
+ if known_first (r) === oneunit (r)
112
+ return get_index_one_to (r, i)
113
+ else
114
+ return get_index_unit_range (r, i)
115
+ end
116
116
end
117
117
118
118
@inline function get_index_one_to (r, i)
119
- @boundscheck if ((i > 0 ) & (i <= last (r)))
120
- throw (BoundsError (r, i))
121
- end
122
- return convert (eltype (r), i)
119
+ @boundscheck if ((i > 0 ) & (i <= last (r)))
120
+ throw (BoundsError (r, i))
121
+ end
122
+ return convert (eltype (r), i)
123
123
end
124
124
125
125
@inline function get_index_unit_range (r, i)
126
- val = first (r) + (i - 1 )
127
- @boundscheck if i > 0 && val <= last (r) && val >= first (r)
128
- throw (BoundsError (r, i))
129
- end
130
- return convert (eltype (r), val)
126
+ val = first (r) + (i - 1 )
127
+ @boundscheck if i > 0 && val <= last (r) && val >= first (r)
128
+ throw (BoundsError (r, i))
129
+ end
130
+ return convert (eltype (r), val)
131
131
end
132
132
133
133
_try_static (x, y) = Val (x)
@@ -141,7 +141,7 @@ _try_static(::Nothing, ::Nothing) = nothing
141
141
@inline function known_length (:: Type{T} ) where {T<: AbstractUnitRange }
142
142
fst = known_first (T)
143
143
lst = known_last (T)
144
- if stp === nothing || fst === nothing || lst === nothing
144
+ if fst === nothing || lst === nothing
145
145
return nothing
146
146
else
147
147
if fst === oneunit (eltype (T))
@@ -153,26 +153,26 @@ _try_static(::Nothing, ::Nothing) = nothing
153
153
end
154
154
155
155
function Base. length (r:: OptionallyStaticUnitRange{T} ) where {T}
156
- if isempty (r)
157
- return zero (T)
156
+ if isempty (r)
157
+ return zero (T)
158
+ else
159
+ if known_one (r) === one (T)
160
+ return unsafe_length_one_to (last (r))
158
161
else
159
- if known_one (r) === one (T)
160
- return unsafe_length_one_to (last (r))
161
- else
162
- return unsafe_length_unit_range (first (r), last (r))
163
- end
162
+ return unsafe_length_unit_range (first (r), last (r))
164
163
end
164
+ end
165
165
end
166
166
167
167
function unsafe_length_unit_range (fst:: T , lst:: T ) where {T<: Union{Int,Int64,Int128} }
168
- return Base. checked_add (Base. checked_sub (lst, fst), one (T))
168
+ return Base. checked_add (Base. checked_sub (lst, fst), one (T))
169
169
end
170
170
function unsafe_length_unit_range (fst:: T , lst:: T ) where {T<: Union{UInt,UInt64,UInt128} }
171
- return Base. checked_add (lst - fst, one (T))
171
+ return Base. checked_add (lst - fst, one (T))
172
172
end
173
173
174
174
"""
175
- indices(x[, d]) -> AbstractRange
175
+ indices(x[, d])
176
176
177
177
Given an array `x`, this returns the indices along dimension `d`. If `x` is a tuple
178
178
of arrays then the indices corresponding to dimension `d` of all arrays in `x` are
@@ -181,12 +181,12 @@ tuple may be used to specify a different dimension for each array. If `d` is not
181
181
specified then indices for visiting each index of `x` is returned.
182
182
"""
183
183
@inline function indices (x)
184
- inds = eachindex (x)
185
- if inds isa AbstractUnitRange{<: Integer }
186
- return Base. Slice (inds)
187
- else
188
- return inds
189
- end
184
+ inds = eachindex (x)
185
+ if inds isa AbstractUnitRange{<: Integer }
186
+ return Base. Slice (inds)
187
+ else
188
+ return inds
189
+ end
190
190
end
191
191
192
192
indices (x, d) = indices (axes (x, d))
@@ -204,11 +204,11 @@ end
204
204
end
205
205
206
206
@inline function _pick_range (x, y)
207
- fst = _try_static (known_first (x), known_first (y))
208
- fst = fst === nothing ? first (x) : fst
207
+ fst = _try_static (known_first (x), known_first (y))
208
+ fst = fst === nothing ? first (x) : fst
209
209
210
- lst = _try_static (known_last (x), known_last (y))
211
- lst = lst === nothing ? last (x) : lst
212
- return Base. Slice (OptionallyStaticUnitRange (fst, lst))
210
+ lst = _try_static (known_last (x), known_last (y))
211
+ lst = lst === nothing ? last (x) : lst
212
+ return Base. Slice (OptionallyStaticUnitRange (fst, lst))
213
213
end
214
214
0 commit comments