@@ -43,7 +43,8 @@ known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
4343# add methods to support ArrayInterface
4444
4545_get (x) = x
46- _get (:: Val{V} ) where {V} = V
46+ _get (:: Static{V} ) where {V} = V
47+ _get (:: Type{Static{V}} ) where {V} = V
4748_convert (:: Type{T} , x) where {T} = convert (T, x)
4849_convert (:: Type{T} , :: Val{V} ) where {T,V} = Val (convert (T, V))
4950
@@ -56,7 +57,7 @@ at compile time. An `OptionallyStaticUnitRange` is intended to be constructed in
5657from other valid indices. Therefore, users should not expect the same checks are used
5758to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
5859"""
59- struct OptionallyStaticUnitRange{T,F,L } <: AbstractUnitRange{T}
60+ struct OptionallyStaticUnitRange{T <: Integer , F <: Integer , L <: Integer } <: AbstractUnitRange{T}
6061 start:: F
6162 stop:: L
6263
@@ -79,28 +80,26 @@ struct OptionallyStaticUnitRange{T,F,L} <: AbstractUnitRange{T}
7980
8081 function OptionallyStaticUnitRange (x:: AbstractRange )
8182 if step (x) == 1
82- fst = known_first (x)
83- fst = fst === nothing ? first (x) : Val (fst)
84- lst = known_last (x)
85- lst = lst === nothing ? last (x) : Val (lst)
83+ fst = static_first (x)
84+ lst = static_last (x)
8685 return OptionallyStaticUnitRange (fst, lst)
8786 else
8887 throw (ArgumentError (" step must be 1, got $(step (r)) " ))
8988 end
9089 end
9190end
9291
93- Base. first (r:: OptionallyStaticUnitRange{<:Any,Val{F}} ) where {F} = F
94- Base. first (r:: OptionallyStaticUnitRange{<:Any,<:Any} ) = r. start
92+ Base.:(:)(L:: Integer , :: Static{U} ) where {U} = OptionallyStaticUnitRange (L, Static (U))
93+ Base.:(:)(:: Static{L} , U:: Integer ) where {L} = OptionallyStaticUnitRange (Static (L), U)
94+ Base.:(:)(:: Static{L} , :: Static{U} ) where {L,U} = OptionallyStaticUnitRange (Static (L), Static (U))
9595
96+ Base. first (r:: OptionallyStaticUnitRange ) = r. start
9697Base. step (r:: OptionallyStaticUnitRange{T} ) where {T} = oneunit (T)
98+ Base. last (r:: OptionallyStaticUnitRange ) = r. stop
9799
98- Base. last (r:: OptionallyStaticUnitRange{<:Any,<:Any,Val{L}} ) where {L} = L
99- Base. last (r:: OptionallyStaticUnitRange{<:Any,<:Any,<:Any} ) = r. stop
100-
101- known_first (:: Type{<:OptionallyStaticUnitRange{<:Any,Val{F}}} ) where {F} = F
100+ known_first (:: Type{<:OptionallyStaticUnitRange{<:Any,Static{F}}} ) where {F} = F
102101known_step (:: Type{<:OptionallyStaticUnitRange{T}} ) where {T} = one (T)
103- known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,<:Any,Val {L}}} ) where {L} = L
102+ known_last (:: Type{<:OptionallyStaticUnitRange{<:Any,<:Any,Static {L}}} ) where {L} = L
104103
105104function Base. isempty (r:: OptionallyStaticUnitRange )
106105 if known_first (r) === oneunit (eltype (r))
@@ -141,10 +140,20 @@ end
141140 return convert (eltype (r), val)
142141end
143142
144- _try_static (x, y) = Val (x)
145- _try_static (:: Nothing , y) = Val (y)
146- _try_static (x, :: Nothing ) = Val (x)
147- _try_static (:: Nothing , :: Nothing ) = nothing
143+ @inline _try_static (:: Static{N} , :: Static{N} ) where {N} = Static {N} ()
144+ @inline _try_static (:: Static{M} , :: Static{N} ) where {M, N} = @assert false " Unequal Indices: Static{$M }() != Static{$N }()"
145+ function _try_static (:: Static{N} , x) where {N}
146+ @assert N == x " Unequal Indices: Static{$N }() != x == $x "
147+ Static {N} ()
148+ end
149+ function _try_static (x, :: Static{N} ) where {N}
150+ @assert N == x " Unequal Indices: x == $x != Static{$N }()"
151+ Static {N} ()
152+ end
153+ function _try_static (x, y)
154+ @assert x == y " Unequal Indicess: x == $x != $y == y"
155+ x
156+ end
148157
149158# ##
150159# ## length
@@ -193,7 +202,7 @@ specified then indices for visiting each index of `x` is returned.
193202"""
194203@inline function indices (x)
195204 inds = eachindex (x)
196- if inds isa AbstractUnitRange{ <: Integer }
205+ if inds isa AbstractUnitRange && eltype (inds) <: Integer
197206 return Base. Slice (OptionallyStaticUnitRange (inds))
198207 else
199208 return inds
@@ -202,30 +211,24 @@ end
202211
203212function indices (x:: Tuple )
204213 inds = map (eachindex, x)
205- @assert all (isequal (first (inds)), Base. tail (inds)) " Not all specified axes are equal: $inds "
206214 return reduce (_pick_range, inds)
207215end
208216
209- indices (x, d) = indices (axes (x, d))
217+ @inline indices (x, d) = indices (axes (x, d))
210218
211- @inline function indices (x:: NTuple{N,<: Any} , dim) where {N}
219+ @inline function indices (x:: Tuple{Vararg{ Any,N} } , dim) where {N}
212220 inds = map (x_i -> indices (x_i, dim), x)
213- @assert all (isequal (first (inds)), Base. tail (inds)) " Not all specified axes are equal: $inds "
214221 return reduce (_pick_range, inds)
215222end
216223
217- @inline function indices (x:: NTuple{N,<: Any} , dim:: NTuple{N,<: Any} ) where {N}
224+ @inline function indices (x:: Tuple{Vararg{ Any,N}} , dim:: Tuple{Vararg{ Any,N} } ) where {N}
218225 inds = map (indices, x, dim)
219- @assert all (isequal (first (inds)), Base. tail (inds)) " Not all specified axes are equal: $inds "
220226 return reduce (_pick_range, inds)
221227end
222228
223229@inline function _pick_range (x, y)
224- fst = _try_static (known_first (x), known_first (y))
225- fst = fst === nothing ? first (x) : fst
226-
227- lst = _try_static (known_last (x), known_last (y))
228- lst = lst === nothing ? last (x) : lst
230+ fst = _try_static (static_first (x), static_first (y))
231+ lst = _try_static (static_last (x), static_last (y))
229232 return Base. Slice (OptionallyStaticUnitRange (fst, lst))
230233end
231234
0 commit comments