Skip to content

Commit 7213c4c

Browse files
authored
buffer method (#195)
See JuliaSIMD/ManualMemory.jl#10 for context Also added parent_type for StaticArrays.SizedArray
1 parent 925fa54 commit 7213c4c

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "3.1.26"
3+
version = "3.1.27"
44

55
[deps]
66
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ArrayInterface.aos_to_soa
4242
ArrayInterface.axes
4343
ArrayInterface.axes_types
4444
ArrayInterface.broadcast_axis
45+
ArrayInterface.buffer
4546
ArrayInterface.canonicalize
4647
ArrayInterface.deleteat
4748
ArrayInterface.dense_dims

src/ArrayInterface.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ has_parent(::Type{T}) where {T} = _has_parent(parent_type(T), T)
8181
_has_parent(::Type{T}, ::Type{T}) where {T} = False()
8282
_has_parent(::Type{T1}, ::Type{T2}) where {T1,T2} = True()
8383

84+
"""
85+
buffer(x)
86+
87+
Return the buffer data that `x` points to. Unlike `parent(x::AbstractArray)`, `buffer(x)`
88+
may not return another array.type.
89+
"""
90+
buffer(x) = parent(x)
91+
buffer(x::SparseMatrixCSC) = getfield(x, :nzval)
92+
buffer(x::SparseVector) = getfield(x, :nzval)
93+
8494
"""
8595
known_length(::Type{T}) -> Union{Int,Nothing}
8696
@@ -699,6 +709,9 @@ function __init__()
699709
ismutable(::Type{<:StaticArrays.MArray}) = true
700710
ismutable(::Type{<:StaticArrays.SizedArray}) = true
701711

712+
buffer(A::Union{StaticArrays.SArray,StaticArrays.MArray}) = getfield(A, :data)
713+
parent_type(::Type{<:StaticArrays.SizedArray{<:Any,<:Any,<:Any,<:Any,P}}) where {P} = P
714+
702715
function lu_instance(_A::StaticArrays.StaticMatrix{N,N}) where {N}
703716
A = StaticArrays.SArray(_A)
704717
L = LowerTriangular(A)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ end
228228
@test parent_type(Diagonal{Int,Vector{Int}}) <: Vector{Int}
229229
@test parent_type(UpperTriangular{Int,Matrix{Int}}) <: Matrix{Int}
230230
@test parent_type(LowerTriangular{Int,Matrix{Int}}) <: Matrix{Int}
231+
@test parent_type(SizedVector{1, Int, Vector{Int}}) <: Vector{Int}
232+
end
233+
234+
@testset "buffer" begin
235+
@test ArrayInterface.buffer(Sp) == [1, 2, 3]
236+
@test ArrayInterface.buffer(sparsevec([1, 2, 0, 0, 3, 0])) == [1, 2, 3]
237+
@test ArrayInterface.buffer(Diagonal([1,2,3])) == [1, 2, 3]
231238
end
232239

233240
include("ranges.jl")

0 commit comments

Comments
 (0)