Skip to content

Commit

Permalink
[WIP] Removing SSubArray in favor of SizedArray (#25)
Browse files Browse the repository at this point in the history
* some work towards removing SSubArray

* improving types of returned values

* update versions

* update codecov CI

* general maintenance

* don't test ArrayInterface compat on older versions of Julia

* more fixing

* one more test
  • Loading branch information
mateuszbaran authored Nov 27, 2020
1 parent 2536759 commit 130fd30
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 813 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ jobs:
arch: x64
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
- uses: codecov/codecov-action@v1
- uses: julia-actions/julia-uploadcodecov@latest
if: ${{ matrix.julia-version == '1.4' && matrix.os =='ubuntu-latest' }}
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HybridArrays"
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
authors = ["Mateusz Baran <[email protected]>"]
version = "0.3.9"
version = "0.4.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -10,7 +10,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Requires = "1"
StaticArrays = "=0.12.5"
StaticArrays = "=1.0.0"
julia = "1"

[extras]
Expand Down
9 changes: 8 additions & 1 deletion src/HybridArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ using Requires
return false
end

function all_dynamic_fixed_val(::Type{Tuple{}})
return Val(:dynamic_fixed_true)
end
function all_dynamic_fixed_val(::Type{Size}) where Size<:Tuple
return error("No indices given for size $Size")
end

function all_dynamic_fixed_val(::Type{Size}, inds::StaticArrays.StaticIndexing{<:Union{Int, AbstractArray, Colon}}...) where Size<:Tuple
return all_dynamic_fixed_val(Size, map(StaticArrays.unwrap, inds)...)
end
Expand Down Expand Up @@ -140,7 +147,7 @@ HybridMatrix{S1,S2,T,M} = HybridArray{Tuple{S1,S2},T,2,M}

export HybridArray, HybridMatrix, HybridVector

include("ssubarray.jl")
include("SSubArray.jl")

include("abstractarray.jl")
include("arraymath.jl")
Expand Down
2 changes: 2 additions & 0 deletions src/SSubArray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

const SSubArray{S,T,N,P,I,L} = SizedArray{S,T,N,N,SubArray{T,N,P,I,L}}
2 changes: 2 additions & 0 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ Base.IndexStyle(a::HybridArray) = Base.IndexStyle(a.data)
Base.IndexStyle(::Type{HA}) where {S,T,N,M,TData,HA<:HybridArray{S,T,N,M,TData}} = Base.IndexStyle(TData)

Base.vec(a::HybridArray) = vec(a.data)

StaticArrays.similar_type(::Type{<:SSubArray},::Type{T},s::Size{S}) where {S,T} = StaticArrays.default_similar_type(T,s,StaticArrays.length_val(s))
18 changes: 11 additions & 7 deletions src/arraymath.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@

function StaticArrays._one(s::Size, ::Type{SM}) where {Sel, N, SM <: HybridArrays.SSubArray{Tuple{N,N}, Sel}}
return StaticArrays._one(s, SMatrix{N,N,Sel})
end

Base.one(A::HA) where {HA<:HybridMatrix} = HA(one(A.data))

@inline function Base.zero(a::HA) where {S, Sel, HA <: SSubArray{S, Sel}}
return StaticArrays.zeros(SArray{S, Sel})
# This should make one(sized subarray) return SArray
@inline function StaticArrays._construct_sametype(a::Type{<:SSubArray{Tuple{S,S},T}}, elements) where {S,T}
return SMatrix{S,S,T}(elements)
end
@inline function StaticArrays._construct_sametype(a::SSubArray{Tuple{S,S},T}, elements) where {S,T}
return SMatrix{S,S,T}(elements)
end

Base.fill!(A::HybridArray, x) = fill!(A.data, x)

function Base.zero(a::HybridArray{S}) where {S}
@inline function Base.zero(a::HybridArray{S}) where {S}
return HybridArray{S}(zero(a.data))
end

@inline function Base.zero(a::SSubArray{S,T}) where {S,T}
return StaticArrays.zeros(SArray{S,T})
end
1 change: 1 addition & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Base.@propagate_inbounds (::Type{HybridArray{S,T}})(a::AbstractArray) where {S,T
@inline convert(::Type{Array}, sa::HybridArray) = convert(Array, sa.data)
@inline convert(::Type{Array{T}}, sa::HybridArray{S,T}) where {T,S} = convert(Array, sa.data)
@inline convert(::Type{Array{T,N}}, sa::HybridArray{S,T,N}) where {T,S,N} = convert(Array, sa.data)
@inline convert(::Type{Array{T,N} where T}, sa::HybridArray{S}) where {S,N} = convert(Array, sa.data)

function check_compatible_sizes(::Type{S}, a::NTuple{N,Int}) where {S,N}
st = size_to_tuple(S)
Expand Down
24 changes: 19 additions & 5 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function _get_indices(i::Tuple, j::Int, i1::Type{Colon}, inds...)
return (i[1], _get_indices(i[2:end], j+1, inds...)...)
end

_totally_linear() = true
_totally_linear(inds...) = false
_totally_linear(inds::Type{Int}...) = true
_totally_linear(inds::Type{Colon}...) = true
Expand Down Expand Up @@ -147,14 +148,10 @@ end
if i == Int
elseif i <: StaticVector
push!(os, i.parameters[1].parameters[1])
elseif i <: Base.Slice{<:StaticVector}
push!(os, i.parameters[1].parameters[1].parameters[1])
elseif i == Colon
elseif i == Colon || i <: Base.Slice
push!(os, s)
elseif i <: SOneTo
push!(os, i.parameters[1])
elseif i <: Base.Slice{<:SOneTo}
push!(os, i.parameters[1].parameters[1])
elseif i <: Base.OneTo || i <: AbstractArray
push!(os, Dynamic())
else
Expand Down Expand Up @@ -282,3 +279,20 @@ end
end
end
end

@inline function _view_hybrid(a::HybridArray{S}, ::Val{:dynamic_fixed_true}, inner_view, indices...) where {S}
new_size = new_out_size(S, indices...)
return SizedArray{new_size}(inner_view)
end

@inline function _view_hybrid(a::HybridArray, ::Val{:dynamic_fixed_false}, inner_view, indices...)
return inner_view
end

@inline function Base.view(
a::HybridArray{S},
indices...,
) where {S}
inner_view = invoke(view, Tuple{AbstractArray, typeof(indices).parameters...}, a, indices...)
return _view_hybrid(a, all_dynamic_fixed_val(S, indices...), inner_view, indices...)
end
2 changes: 1 addition & 1 deletion src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const HybridVecOrMatLike{T} = Union{HybridVector{<:Any, T}, HybridMatrixLike{T}}
end

@inline hcat(a::HybridVecOrMatLike, b::HybridVecOrMatLike) = _hcat(Size(a), Size(b), a, b)
@inline hcat(a::HybridVecOrMatLike, b::HybridVecOrMatLike, c::HybridVecOrMatLike...) = hcat(hcat(a,b), hcat(c...))
@inline hcat(a::HybridVecOrMatLike, b::HybridVecOrMatLike, c::HybridVecOrMatLike...) = hcat(hcat(a,b), c...)

# used in _vcat and _hcat
@inline +(::Dynamic, ::Dynamic) = Dynamic()
Expand Down
Loading

2 comments on commit 130fd30

@mateuszbaran
Copy link
Collaborator 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/25418

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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" 130fd3020d9613ebad71d4c87467330afd7fe54f
git push origin v0.4.0

Please sign in to comment.