Skip to content

Commit 0630508

Browse files
authored
Move ranges from ArrayInterface to Static (#88)
* Move ranges from ArrayInterface to Static * Use breaking version so that ArrayInterface ranges are compatible * static_promote for ranges * Add `IntType` union and function This replaces the concept of "canonical" integers in ArrayInterface so we can also move that functionality here.
1 parent a47c525 commit 0630508

File tree

5 files changed

+562
-1
lines changed

5 files changed

+562
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Static"
22
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
33
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
4-
version = "0.7.8"
4+
version = "0.8"
55

66
[deps]
77
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"

src/Static.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ struct StaticInt{N} <: StaticInteger{N}
6161
StaticInt(::Val{N}) where {N} = StaticInt(N)
6262
end
6363

64+
"""
65+
IntType(x::Integer) -> Union{Int,StaticInt}
66+
67+
`IntType` is a union of `Int` and `StaticInt`. As a function, it ensures that `x` one of the
68+
two.
69+
"""
70+
const IntType = Union{StaticInt, Int}
71+
IntType(x::Integer) = Int(x)
72+
IntType(@nospecialize x::Union{Int, StaticInt}) = x
73+
6474
include("float.jl")
6575

6676
const StaticNumber{N} = Union{StaticInt{N}, StaticBool{N}, StaticFloat64{N}}
@@ -260,6 +270,36 @@ _static_promote(::Nothing, ::Nothing) = nothing
260270
_static_promote(x, ::Nothing) = x
261271
_static_promote(::Nothing, y) = y
262272

273+
"""
274+
static_promote(x::AbstractRange{<:Integer}, y::AbstractRange{<:Integer})
275+
276+
A type stable method for combining two equal ranges into a new range that preserves static
277+
parameters. Throws an error if `x != y`.
278+
279+
# Examples
280+
281+
```julia
282+
julia> static_promote(static(1):10, 1:static(10))
283+
static(1):static(10)
284+
285+
julia> static_promote(1:2:9, static(1):static(2):static(9))
286+
static(1):static(2):static(9)
287+
```
288+
"""
289+
Base.@propagate_inbounds @inline function static_promote(x::AbstractUnitRange{<:Integer},
290+
y::AbstractUnitRange{<:Integer})
291+
fst = static_promote(static_first(x), static_first(y))
292+
lst = static_promote(static_last(x), static_last(y))
293+
return OptionallyStaticUnitRange(fst, lst)
294+
end
295+
Base.@propagate_inbounds @inline function static_promote(x::AbstractRange{<:Integer},
296+
y::AbstractRange{<:Integer})
297+
fst = static_promote(static_first(x), static_first(y))
298+
stp = static_promote(static_step(x), static_step(y))
299+
lst = static_promote(static_last(x), static_last(y))
300+
return _OptionallyStaticStepRange(fst, stp, lst)
301+
end
302+
263303
Base.@propagate_inbounds function _promote_shape(a::Tuple{A, Vararg{Any}},
264304
b::Tuple{B, Vararg{Any}}) where {A, B}
265305
(static_promote(getfield(a, 1), getfield(b, 1)),
@@ -879,4 +919,6 @@ function Base.show(io::IO, m::MIME"text/plain", @nospecialize(x::NDIndex))
879919
nothing
880920
end
881921

922+
include("ranges.jl")
923+
882924
end

0 commit comments

Comments
 (0)