Skip to content

Commit

Permalink
Support conversion of more types between static/dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Mar 4, 2020
1 parent 301a7a3 commit a07d82c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StaticRanges"
uuid = "d8176aec-3168-11e9-3c98-e3954798be3a"
authors = ["zchristensen "]
version = "0.5.4"
version = "0.5.5"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
26 changes: 25 additions & 1 deletion src/staticness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ true
end
is_static(::Type{T}) where {T<:Tuple} = true
is_static(::Type{T}) where {T<:NamedTuple} = true
is_static(::Type{T}) where {T<:StaticArray} = true

"""
is_fixed(x) -> Bool
Expand Down Expand Up @@ -203,6 +204,14 @@ as_dynamic(x::Union{LinRange,LinSRange}) = LinMRange(first(x), last(x), length(x
as_dynamic(x::StepMRangeLen) = x
as_dynamic(x::Union{StepRangeLen,StepSRangeLen}) = StepMRangeLen(first(x), step(x), length(x), x.offset)

function as_dynamic(A::AbstractArray)
if is_dynamic(A)
return A
else
return Array(A)
end
end


"""
as_fixed(x)
Expand Down Expand Up @@ -254,7 +263,7 @@ type to `x`.
## Examples
```jldoctest
julia> using StaticRanges
julia> using StaticRanges, StaticArrays
julia> as_static(Base.OneTo(10))
OneToSRange(10)
Expand All @@ -270,6 +279,13 @@ StepSRangeLen(1.0:2.0:19.0)
julia> as_static(LinRange(1, 20, 10))
LinSRange(1.0, stop=20.0, length=10)
julia> as_static(reshape(1:12, (3, 4)))
3×4 SArray{Tuple{3,4},Int64,2,12} with indices SOneTo(3)×SOneTo(4):
1 4 7 10
2 5 8 11
3 6 9 12
```
"""
as_static(x::OneToSRange) = x
Expand All @@ -287,3 +303,11 @@ as_static(x::Union{LinRange,LinMRange}) = LinSRange(first(x), last(x), length(x)
as_static(x::StepSRangeLen) = x
as_static(x::Union{StepRangeLen,StepMRangeLen}) = StepSRangeLen(first(x), step(x), length(x), x.offset)

function as_static(A::AbstractArray)
if is_static(A)
return A
else
return SArray{Tuple{size(A)...}}(A)
end
end

2 comments on commit a07d82c

@Tokazama
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/10530

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.5 -m "<description of version>" a07d82c9c05ffa26db89e9a4370a49295df9927c
git push origin v0.5.5

Please sign in to comment.