@@ -43,7 +43,8 @@ known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
43
43
# add methods to support ArrayInterface
44
44
45
45
_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
47
48
_convert (:: Type{T} , x) where {T} = convert (T, x)
48
49
_convert (:: Type{T} , :: Val{V} ) where {T,V} = Val (convert (T, V))
49
50
@@ -56,7 +57,7 @@ at compile time. An `OptionallyStaticUnitRange` is intended to be constructed in
56
57
from other valid indices. Therefore, users should not expect the same checks are used
57
58
to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
58
59
"""
59
- struct OptionallyStaticUnitRange{T,F,L } <: AbstractUnitRange{T}
60
+ struct OptionallyStaticUnitRange{T <: Integer , F <: Integer , L <: Integer } <: AbstractUnitRange{T}
60
61
start:: F
61
62
stop:: L
62
63
@@ -79,28 +80,26 @@ struct OptionallyStaticUnitRange{T,F,L} <: AbstractUnitRange{T}
79
80
80
81
function OptionallyStaticUnitRange (x:: AbstractRange )
81
82
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)
86
85
return OptionallyStaticUnitRange (fst, lst)
87
86
else
88
87
throw (ArgumentError (" step must be 1, got $(step (r)) " ))
89
88
end
90
89
end
91
90
end
92
91
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))
95
95
96
+ Base. first (r:: OptionallyStaticUnitRange ) = r. start
96
97
Base. step (r:: OptionallyStaticUnitRange{T} ) where {T} = oneunit (T)
98
+ Base. last (r:: OptionallyStaticUnitRange ) = r. stop
97
99
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
102
101
known_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
104
103
105
104
function Base. isempty (r:: OptionallyStaticUnitRange )
106
105
if known_first (r) === oneunit (eltype (r))
@@ -141,10 +140,20 @@ end
141
140
return convert (eltype (r), val)
142
141
end
143
142
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
148
157
149
158
# ##
150
159
# ## length
@@ -193,7 +202,7 @@ specified then indices for visiting each index of `x` is returned.
193
202
"""
194
203
@inline function indices (x)
195
204
inds = eachindex (x)
196
- if inds isa AbstractUnitRange{ <: Integer }
205
+ if inds isa AbstractUnitRange && eltype (inds) <: Integer
197
206
return Base. Slice (OptionallyStaticUnitRange (inds))
198
207
else
199
208
return inds
@@ -202,30 +211,24 @@ end
202
211
203
212
function indices (x:: Tuple )
204
213
inds = map (eachindex, x)
205
- @assert all (isequal (first (inds)), Base. tail (inds)) " Not all specified axes are equal: $inds "
206
214
return reduce (_pick_range, inds)
207
215
end
208
216
209
- indices (x, d) = indices (axes (x, d))
217
+ @inline indices (x, d) = indices (axes (x, d))
210
218
211
- @inline function indices (x:: NTuple{N,<: Any} , dim) where {N}
219
+ @inline function indices (x:: Tuple{Vararg{ Any,N} } , dim) where {N}
212
220
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 "
214
221
return reduce (_pick_range, inds)
215
222
end
216
223
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}
218
225
inds = map (indices, x, dim)
219
- @assert all (isequal (first (inds)), Base. tail (inds)) " Not all specified axes are equal: $inds "
220
226
return reduce (_pick_range, inds)
221
227
end
222
228
223
229
@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))
229
232
return Base. Slice (OptionallyStaticUnitRange (fst, lst))
230
233
end
231
234
0 commit comments