Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Static"
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
version = "0.7.8"
version = "0.8"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
42 changes: 42 additions & 0 deletions src/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ struct StaticInt{N} <: StaticInteger{N}
StaticInt(::Val{N}) where {N} = StaticInt(N)
end

"""
IntType(x::Integer) -> Union{Int,StaticInt}

`IntType` is a union of `Int` and `StaticInt`. As a function, it ensures that `x` one of the
two.
"""
const IntType = Union{StaticInt, Int}
IntType(x::Integer) = Int(x)
IntType(@nospecialize x::Union{Int, StaticInt}) = x

include("float.jl")

const StaticNumber{N} = Union{StaticInt{N}, StaticBool{N}, StaticFloat64{N}}
Expand Down Expand Up @@ -260,6 +270,36 @@ _static_promote(::Nothing, ::Nothing) = nothing
_static_promote(x, ::Nothing) = x
_static_promote(::Nothing, y) = y

"""
static_promote(x::AbstractRange{<:Integer}, y::AbstractRange{<:Integer})

A type stable method for combining two equal ranges into a new range that preserves static
parameters. Throws an error if `x != y`.

# Examples

```julia
julia> static_promote(static(1):10, 1:static(10))
static(1):static(10)

julia> static_promote(1:2:9, static(1):static(2):static(9))
static(1):static(2):static(9)
```
"""
Base.@propagate_inbounds @inline function static_promote(x::AbstractUnitRange{<:Integer},
y::AbstractUnitRange{<:Integer})
fst = static_promote(static_first(x), static_first(y))
lst = static_promote(static_last(x), static_last(y))
return OptionallyStaticUnitRange(fst, lst)
end
Base.@propagate_inbounds @inline function static_promote(x::AbstractRange{<:Integer},
y::AbstractRange{<:Integer})
fst = static_promote(static_first(x), static_first(y))
stp = static_promote(static_step(x), static_step(y))
lst = static_promote(static_last(x), static_last(y))
return _OptionallyStaticStepRange(fst, stp, lst)
end

Base.@propagate_inbounds function _promote_shape(a::Tuple{A, Vararg{Any}},
b::Tuple{B, Vararg{Any}}) where {A, B}
(static_promote(getfield(a, 1), getfield(b, 1)),
Expand Down Expand Up @@ -879,4 +919,6 @@ function Base.show(io::IO, m::MIME"text/plain", @nospecialize(x::NDIndex))
nothing
end

include("ranges.jl")

end
Loading