Skip to content

Commit e58d0ca

Browse files
authored
Solving #18 (#19)
* zero implemented for HybridArray * forwarding IndexStyle of wrapped array * more tests for IndexStyle * trying testing on Julia 1.5 rc * implementing ArrayInterface * one more test * making ArrayInterface optional * implementing vec * one more test for vec
1 parent 7d5735e commit e58d0ca

9 files changed

+69
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ${{ matrix.os }}
1212
strategy:
1313
matrix:
14-
julia-version: [1.0, 1.1, 1.2, 1.3, 1.4]
14+
julia-version: [1.0, 1.1, 1.2, 1.3, 1.4, '~1.5.0-0']
1515
os: [ubuntu-latest, macOS-latest, windows-latest]
1616
steps:
1717
- uses: actions/checkout@v2

Project.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
name = "HybridArrays"
22
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
33
authors = ["Mateusz Baran <mateuszbaran89@gmail.com>"]
4-
version = "0.3.6"
4+
version = "0.3.7"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8+
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
89
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
910

1011
[compat]
12+
Requires = "1"
1113
StaticArrays = "=0.12.4"
1214
julia = "1"
1315

1416
[extras]
17+
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
1518
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1619
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1720

1821
[targets]
19-
test = ["Test", "Random"]
22+
test = ["Test", "Random", "ArrayInterface"]

src/HybridArrays.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ using StaticArrays: Dynamic
2424
import StaticArrays: _setindex!_scalar, Size
2525

2626
using LinearAlgebra
27+
using Requires
2728

2829

2930
@generated function hasdynamic(::Type{Size}) where Size<:Tuple
@@ -149,4 +150,10 @@ include("indexing.jl")
149150
include("linalg.jl")
150151
include("utils.jl")
151152

153+
function __init__()
154+
@require ArrayInterface="4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" begin
155+
include("array_interface_compat.jl")
156+
end
157+
end
158+
152159
end # module

src/abstractarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ _h_similar_type(::Type{A},::Type{T},s::Size{S}) where {A<:HybridArray,T,S} = hyb
4343

4444

4545
Size(::Type{<:HybridArray{S}}) where {S} = Size(S)
46+
47+
Base.IndexStyle(a::HybridArray) = Base.IndexStyle(a.data)
48+
Base.IndexStyle(::Type{HA}) where {S,T,N,M,TData,HA<:HybridArray{S,T,N,M,TData}} = Base.IndexStyle(TData)
49+
50+
Base.vec(a::HybridArray) = vec(a.data)

src/array_interface_compat.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
function ArrayInterface.ismutable(::Type{HybridArray{S,T,N,M,TData}}) where {S,T,N,M,TData}
3+
return ArrayInterface.ismutable(TData)
4+
end
5+
6+
function ArrayInterface.can_setindex(::Type{HybridArray{S,T,N,M,TData}}) where {S,T,N,M,TData}
7+
return ArrayInterface.can_setindex(TData)
8+
end
9+
10+
function ArrayInterface.parent_type(::Type{HybridArray{S,T,N,M,TData}}) where {S,T,N,M,TData}
11+
return TData
12+
end
13+
14+
function ArrayInterface.restructure(x::HybridArray{S}, y) where {S}
15+
return HybridArray{S}(reshape(convert(Array, y), size(x)...))
16+
end

src/arraymath.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ Base.one(A::HA) where {HA<:HybridMatrix} = HA(one(A.data))
1010
end
1111

1212
Base.fill!(A::HybridArray, x) = fill!(A.data, x)
13+
14+
function Base.zero(a::HybridArray{S}) where {S}
15+
return HybridArray{S}(zero(a.data))
16+
end

test/abstractarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,22 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra
4141
@test isa(@inferred(similar(M, Float64)), HybridMatrix{2, StaticArrays.Dynamic(), Float64})
4242
end
4343

44+
@testset "IndexStyle" begin
45+
M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4])
46+
MT = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]')
47+
@test (@inferred IndexStyle(M)) === IndexLinear()
48+
@test (@inferred IndexStyle(MT)) === IndexCartesian()
49+
@test (@inferred IndexStyle(typeof(M))) === IndexLinear()
50+
@test (@inferred IndexStyle(typeof(MT))) === IndexCartesian()
51+
end
52+
53+
@testset "vec" begin
54+
M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4])
55+
Mv = vec(M)
56+
@test Mv == [1, 3, 2, 4]
57+
@test Mv isa Vector{Int}
58+
Mv[2] = 100
59+
@test M[2, 1] == 100
60+
end
61+
4462
end

test/array_interface_compat.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
using HybridArrays, ArrayInterface, Test, StaticArrays
3+
4+
@testset "ArrayInterface compatibility" begin
5+
M = HybridMatrix{2, StaticArrays.Dynamic()}([1 2; 4 5])
6+
@test ArrayInterface.ismutable(M)
7+
@test ArrayInterface.can_setindex(M)
8+
@test ArrayInterface.parent_type(M) === Matrix{Int}
9+
@test ArrayInterface.restructure(M, [2, 4, 6, 8]) == HybridMatrix{2, StaticArrays.Dynamic()}([2 6; 4 8])
10+
@test isa(ArrayInterface.restructure(M, [2, 4, 6, 8]), HybridMatrix{2, StaticArrays.Dynamic()})
11+
end

test/arraymath.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra
1313
@testset "zero()" begin
1414
M = HybridMatrix{2, StaticArrays.Dynamic()}([1 2; 4 5])
1515
@test (@inferred zero(M)) == @SMatrix [0 0; 0 0]
16-
@test isa(one(M), HybridMatrix{2,StaticArrays.Dynamic(),Int})
16+
@test isa(zero(M), HybridMatrix{2,StaticArrays.Dynamic(),Int})
1717

1818
Mv = view(M, :, SOneTo(2))
1919
@test (@inferred zero(Mv)) === @SMatrix [0 0; 0 0]
20+
2021
end
2122

2223
@testset "fill!()" begin

0 commit comments

Comments
 (0)