@@ -58,25 +58,25 @@ from other valid indices. Therefore, users should not expect the same checks are
5858to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
5959"""
6060struct 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)
7473 end
74+ end
7575
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
8080end
8181
8282Base. first (r:: OptionallyStaticUnitRange{<:Any,Val{F}} ) where {F} = F
@@ -92,11 +92,11 @@ known_step(::Type{<:OptionallyStaticUnitRange{T}}) where {T} = one(T)
9292known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,<:Any,Val{L}}} ) where {L} = L
9393
9494function 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
100100end
101101
102102unsafe_isempty_one_to (lst) = lst <= zero (lst)
@@ -108,26 +108,26 @@ unsafe_length_one_to(lst::T) where {T<:Int} = T(lst)
108108unsafe_length_one_to (lst:: T ) where {T} = Integer (lst - zero (lst))
109109
110110Base. @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
116116end
117117
118118@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)
123123end
124124
125125@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)
131131end
132132
133133_try_static (x, y) = Val (x)
@@ -141,7 +141,7 @@ _try_static(::Nothing, ::Nothing) = nothing
141141@inline function known_length (:: Type{T} ) where {T<: AbstractUnitRange }
142142 fst = known_first (T)
143143 lst = known_last (T)
144- if stp === nothing || fst === nothing || lst === nothing
144+ if fst === nothing || lst === nothing
145145 return nothing
146146 else
147147 if fst === oneunit (eltype (T))
@@ -153,26 +153,26 @@ _try_static(::Nothing, ::Nothing) = nothing
153153end
154154
155155function 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))
158161 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))
164163 end
164+ end
165165end
166166
167167function 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))
169169end
170170function 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))
172172end
173173
174174"""
175- indices(x[, d]) -> AbstractRange
175+ indices(x[, d])
176176
177177Given an array `x`, this returns the indices along dimension `d`. If `x` is a tuple
178178of 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
181181specified then indices for visiting each index of `x` is returned.
182182"""
183183@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
190190end
191191
192192indices (x, d) = indices (axes (x, d))
@@ -204,11 +204,11 @@ end
204204end
205205
206206@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
209209
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))
213213end
214214
0 commit comments