@@ -42,12 +42,6 @@ known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
42
42
43
43
# add methods to support ArrayInterface
44
44
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
-
51
45
"""
52
46
OptionallyStaticUnitRange{T<:Integer}(start, stop) <: OrdinalRange{T,T}
53
47
@@ -57,28 +51,23 @@ at compile time. An `OptionallyStaticUnitRange` is intended to be constructed in
57
51
from other valid indices. Therefore, users should not expect the same checks are used
58
52
to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
59
53
"""
60
- struct OptionallyStaticUnitRange{T <: Integer , F <: Integer , L <: Integer } <: AbstractUnitRange{T }
54
+ struct OptionallyStaticUnitRange{F <: Integer , L <: Integer } <: AbstractUnitRange{Int }
61
55
start:: F
62
56
stop:: L
63
57
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)
68
62
else
69
- return OptionallyStaticUnitRange {T} (start, _convert (T, stop))
63
+ return OptionallyStaticUnitRange (start, Int ( stop))
70
64
end
71
65
else
72
- return OptionallyStaticUnitRange {T} ( _convert (T, start), stop)
66
+ return OptionallyStaticUnitRange ( Int ( start), stop)
73
67
end
74
68
end
75
69
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 )
82
71
if step (x) == 1
83
72
fst = static_first (x)
84
73
lst = static_last (x)
@@ -94,12 +83,12 @@ Base.:(:)(::Static{L}, U::Integer) where {L} = OptionallyStaticUnitRange(Static(
94
83
Base.:(:)(:: Static{L} , :: Static{U} ) where {L,U} = OptionallyStaticUnitRange (Static (L), Static (U))
95
84
96
85
Base. first (r:: OptionallyStaticUnitRange ) = r. start
97
- Base. step (r :: OptionallyStaticUnitRange{T} ) where {T} = oneunit (T )
86
+ Base. step (:: OptionallyStaticUnitRange ) = Static ( 1 )
98
87
Base. last (r:: OptionallyStaticUnitRange ) = r. stop
99
88
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
103
92
104
93
function Base. isempty (r:: OptionallyStaticUnitRange )
105
94
if known_first (r) === oneunit (eltype (r))
112
101
unsafe_isempty_one_to (lst) = lst <= zero (lst)
113
102
unsafe_isempty_unit_range (fst, lst) = fst > lst
114
103
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
119
106
120
107
Base. @propagate_inbounds function Base. getindex (r:: OptionallyStaticUnitRange , i:: Integer )
121
108
if known_first (r) === oneunit (r)
@@ -144,15 +131,15 @@ end
144
131
@inline _try_static (:: Static{M} , :: Static{N} ) where {M, N} = @assert false " Unequal Indices: Static{$M }() != Static{$N }()"
145
132
function _try_static (:: Static{N} , x) where {N}
146
133
@assert N == x " Unequal Indices: Static{$N }() != x == $x "
147
- Static {N} ()
134
+ return Static {N} ()
148
135
end
149
136
function _try_static (x, :: Static{N} ) where {N}
150
137
@assert N == x " Unequal Indices: x == $x != Static{$N }()"
151
- Static {N} ()
138
+ return Static {N} ()
152
139
end
153
140
function _try_static (x, y)
154
141
@assert x == y " Unequal Indicess: x == $x != $y == y"
155
- x
142
+ return x
156
143
end
157
144
158
145
# ##
@@ -172,24 +159,19 @@ end
172
159
end
173
160
end
174
161
175
- function Base. length (r:: OptionallyStaticUnitRange{T} ) where {T}
162
+ function Base. length (r:: OptionallyStaticUnitRange )
176
163
if isempty (r)
177
- return zero (T)
164
+ return 0
178
165
else
179
- if known_one (r) === one (T)
166
+ if known_first (r) === 0
180
167
return unsafe_length_one_to (last (r))
181
168
else
182
169
return unsafe_length_unit_range (first (r), last (r))
183
170
end
184
171
end
185
172
end
186
173
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 )
193
175
194
176
"""
195
177
indices(x[, d])
231
213
lst = _try_static (static_last (x), static_last (y))
232
214
return Base. Slice (OptionallyStaticUnitRange (fst, lst))
233
215
end
234
-
0 commit comments