Skip to content

Commit 5c6ac4b

Browse files
committed
Clean up OptionallyStaticUnitRange implementation
* `Val` isn't supported anymore so remove related methods (e.g., `_get`) * `Static` can only be an `Int`. If either the first or last field is `Static` then eltype must but `Int`, so might as well guarantee that it is an `Int` type.
1 parent a486f64 commit 5c6ac4b

File tree

1 file changed

+22
-40
lines changed

1 file changed

+22
-40
lines changed

src/ranges.jl

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5751
from other valid indices. Therefore, users should not expect the same checks are used
5852
to 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(
9483
Base.:(:)(::Static{L}, ::Static{U}) where {L,U} = OptionallyStaticUnitRange(Static(L), Static(U))
9584

9685
Base.first(r::OptionallyStaticUnitRange) = r.start
97-
Base.step(r::OptionallyStaticUnitRange{T}) where {T} = oneunit(T)
86+
Base.step(::OptionallyStaticUnitRange) = Static(1)
9887
Base.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

10493
function Base.isempty(r::OptionallyStaticUnitRange)
10594
if known_first(r) === oneunit(eltype(r))
@@ -112,9 +101,8 @@ end
112101
unsafe_isempty_one_to(lst) = lst <= zero(lst)
113102
unsafe_isempty_unit_range(fst, lst) = fst > lst
114103

115-
116-
unsafe_length_one_to(lst::T) where {T<:Int} = T(lst)
117-
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
118106

119107
Base.@propagate_inbounds function Base.getindex(r::OptionallyStaticUnitRange, i::Integer)
120108
if known_first(r) === oneunit(r)
@@ -143,15 +131,15 @@ end
143131
@inline _try_static(::Static{M}, ::Static{N}) where {M, N} = @assert false "Unequal Indices: Static{$M}() != Static{$N}()"
144132
function _try_static(::Static{N}, x) where {N}
145133
@assert N == x "Unequal Indices: Static{$N}() != x == $x"
146-
Static{N}()
134+
return Static{N}()
147135
end
148136
function _try_static(x, ::Static{N}) where {N}
149137
@assert N == x "Unequal Indices: x == $x != Static{$N}()"
150-
Static{N}()
138+
return Static{N}()
151139
end
152140
function _try_static(x, y)
153141
@assert x == y "Unequal Indicess: x == $x != $y == y"
154-
x
142+
return x
155143
end
156144

157145
###
@@ -171,24 +159,20 @@ end
171159
end
172160
end
173161

174-
function Base.length(r::OptionallyStaticUnitRange{T}) where {T}
162+
function Base.length(r::OptionallyStaticUnitRange)
175163
if isempty(r)
176-
return zero(T)
164+
return 0
177165
else
178-
if known_first(r) === one(T)
166+
if known_first(r) === 0
179167
return unsafe_length_one_to(last(r))
180168
else
181169
return unsafe_length_unit_range(first(r), last(r))
182170
end
183171
end
184172
end
185173

186-
unsafe_length_unit_range(fst, lst) = Integer(lst - fst + 1)
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))
174+
function unsafe_length_unit_range(start, stop)
175+
return Base.checked_add(Base.checked_sub(start, stop), one(T))
192176
end
193177

194178
"""
@@ -231,5 +215,3 @@ end
231215
lst = _try_static(static_last(x), static_last(y))
232216
return Base.Slice(OptionallyStaticUnitRange(fst, lst))
233217
end
234-
235-
indices(::Tuple{Vararg{Any,N}}) where {N} = OptionallyStaticUnitRange(Static(1), Static(N))

0 commit comments

Comments
 (0)