Skip to content

Commit 8b77542

Browse files
mateuszbaranc42f
authored andcommitted
Base.strides methods for statically sized arrays (#658)
Having Base.strides available is helpful for generic code which needs to understand the underlying strided memory layout (eg to pass to lapack).
1 parent 935dc85 commit 8b77542

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/abstractarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ end
1616
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1717
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1818

19+
# Base.strides is intentionally not defined for SArray, see PR #658 for discussion
20+
Base.strides(a::MArray) = Base.size_to_strides(1, size(a)...)
21+
Base.strides(a::SizedArray) = strides(a.data)
22+
1923
function Base.summary(io::IO, a, inds::Tuple{SOneTo, Vararg{SOneTo}})
2024
print(io, Base.dims2string(length.(inds)), " ")
2125
Base.showarg(io, a, true)

test/abstractarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ using StaticArrays, Test, LinearAlgebra
99
@test Base.isassigned(m, 2, 2) == true
1010
end
1111

12+
@testset "strides" begin
13+
m1 = MArray{Tuple{3, 4, 5}}(rand(Int, 3, 4, 5))
14+
m2 = Size(3,4,5)(rand(Int, 3, 4, 5))
15+
@test strides(m1) === (1, 3, 12)
16+
@test strides(m2) === (1, 3, 12)
17+
end
18+
1219
@testset "similar_type" begin
1320
@test @inferred(similar_type(SVector{3,Int})) == SVector{3,Int}
1421
@test @inferred(similar_type(@SVector [1,2,3])) == SVector{3,Int}

0 commit comments

Comments
 (0)