@@ -42,12 +42,6 @@ known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
4242
4343# add methods to support ArrayInterface
4444
45- _get (x) = x
46- _get (:: Static{V} ) where {V} = V
47- _get (:: Type{Static{V}} ) where {V} = V
48- _convert (:: Type{T} , x) where {T} = convert (T, x)
49- _convert (:: Type{T} , :: Val{V} ) where {T,V} = Val (convert (T, V))
50-
5145"""
5246 OptionallyStaticUnitRange{T<:Integer}(start, stop) <: OrdinalRange{T,T}
5347
@@ -57,28 +51,23 @@ at compile time. An `OptionallyStaticUnitRange` is intended to be constructed in
5751from other valid indices. Therefore, users should not expect the same checks are used
5852to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
5953"""
60- struct OptionallyStaticUnitRange{T <: Integer , F <: Integer , L <: Integer } <: AbstractUnitRange{T }
54+ struct OptionallyStaticUnitRange{F <: Integer , L <: Integer } <: AbstractUnitRange{Int }
6155 start:: F
6256 stop:: L
6357
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)
58+ function OptionallyStaticUnitRange (start, stop)
59+ if eltype (start) <: Int
60+ if eltype (stop) <: Int
61+ return new {typeof(start),typeof(stop)} (start, stop)
6862 else
69- return OptionallyStaticUnitRange {T} (start, _convert (T, stop))
63+ return OptionallyStaticUnitRange (start, Int ( stop))
7064 end
7165 else
72- return OptionallyStaticUnitRange {T} ( _convert (T, start), stop)
66+ return OptionallyStaticUnitRange ( Int ( start), stop)
7367 end
7468 end
7569
76- function OptionallyStaticUnitRange (start, stop)
77- T = promote_type (typeof (_get (start)), typeof (_get (stop)))
78- return OptionallyStaticUnitRange {T} (start, stop)
79- end
80-
81- function OptionallyStaticUnitRange (x:: AbstractRange )
70+ function OptionallyStaticUnitRange (x:: AbstractRange )
8271 if step (x) == 1
8372 fst = static_first (x)
8473 lst = static_last (x)
@@ -94,12 +83,12 @@ Base.:(:)(::Static{L}, U::Integer) where {L} = OptionallyStaticUnitRange(Static(
9483Base.:(:)(:: Static{L} , :: Static{U} ) where {L,U} = OptionallyStaticUnitRange (Static (L), Static (U))
9584
9685Base. first (r:: OptionallyStaticUnitRange ) = r. start
97- Base. step (r :: OptionallyStaticUnitRange{T} ) where {T} = oneunit (T )
86+ Base. step (:: OptionallyStaticUnitRange ) = Static ( 1 )
9887Base. last (r:: OptionallyStaticUnitRange ) = r. stop
9988
100- known_first (:: Type{<:OptionallyStaticUnitRange{<:Any, Static{F}}} ) where {F} = F
101- known_step (:: Type{<:OptionallyStaticUnitRange{T}} ) where {T} = one (T)
102- known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,<:Any, Static{L}}} ) where {L} = L
89+ known_first (:: Type{<:OptionallyStaticUnitRange{Static{F}}} ) where {F} = F
90+ known_step (:: Type{<:OptionallyStaticUnitRange} ) = 1
91+ known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,Static{L}}} ) where {L} = L
10392
10493function Base. isempty (r:: OptionallyStaticUnitRange )
10594 if known_first (r) === oneunit (eltype (r))
112101unsafe_isempty_one_to (lst) = lst <= zero (lst)
113102unsafe_isempty_unit_range (fst, lst) = fst > lst
114103
115- unsafe_isempty_unit_range (fst:: T , lst:: T ) where {T} = Integer (lst - fst + one (T))
116-
117- unsafe_length_one_to (lst:: T ) where {T<: Int } = T (lst)
118- unsafe_length_one_to (lst:: T ) where {T} = Integer (lst - zero (lst))
104+ unsafe_length_one_to (lst:: Int ) = lst
105+ unsafe_length_one_to (:: Static{L} ) where {L} = lst
119106
120107Base. @propagate_inbounds function Base. getindex (r:: OptionallyStaticUnitRange , i:: Integer )
121108 if known_first (r) === oneunit (r)
@@ -144,15 +131,15 @@ end
144131@inline _try_static (:: Static{M} , :: Static{N} ) where {M, N} = @assert false " Unequal Indices: Static{$M }() != Static{$N }()"
145132function _try_static (:: Static{N} , x) where {N}
146133 @assert N == x " Unequal Indices: Static{$N }() != x == $x "
147- Static {N} ()
134+ return Static {N} ()
148135end
149136function _try_static (x, :: Static{N} ) where {N}
150137 @assert N == x " Unequal Indices: x == $x != Static{$N }()"
151- Static {N} ()
138+ return Static {N} ()
152139end
153140function _try_static (x, y)
154141 @assert x == y " Unequal Indicess: x == $x != $y == y"
155- x
142+ return x
156143end
157144
158145# ##
@@ -172,24 +159,19 @@ end
172159 end
173160end
174161
175- function Base. length (r:: OptionallyStaticUnitRange{T} ) where {T}
162+ function Base. length (r:: OptionallyStaticUnitRange )
176163 if isempty (r)
177- return zero (T)
164+ return 0
178165 else
179- if known_one (r) === one (T)
166+ if known_first (r) === 0
180167 return unsafe_length_one_to (last (r))
181168 else
182169 return unsafe_length_unit_range (first (r), last (r))
183170 end
184171 end
185172end
186173
187- function unsafe_length_unit_range (fst:: T , lst:: T ) where {T<: Union{Int,Int64,Int128} }
188- return Base. checked_add (Base. checked_sub (lst, fst), one (T))
189- end
190- function unsafe_length_unit_range (fst:: T , lst:: T ) where {T<: Union{UInt,UInt64,UInt128} }
191- return Base. checked_add (lst - fst, one (T))
192- end
174+ unsafe_length_unit_range (start:: Integer , stop:: Integer ) = Int (start - stop + 1 )
193175
194176"""
195177 indices(x[, d])
231213 lst = _try_static (static_last (x), static_last (y))
232214 return Base. Slice (OptionallyStaticUnitRange (fst, lst))
233215end
234-
0 commit comments