Skip to content

Commit 99007c7

Browse files
Support resizedata! and cacheddata for Vcat (#386)
* Support resizedata! and cacheddata for Vcat * Functionality + more tests * EOL * Revert paddedlayout vcat --------- Co-authored-by: Daniel VandenHeuvel <[email protected]>
1 parent 7a146a8 commit 99007c7

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/cache.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ cache_layout(::AbstractStridedLayout, O::AbstractArray) = copy(O)
5858
const _cache = cache_layout # TODO: deprecate
5959
cacheddata(A::AbstractCachedArray) = view(A.data,OneTo.(A.datasize)...)
6060

61+
maybe_cacheddata(A::AbstractCachedArray) = cacheddata(A)
62+
maybe_cacheddata(A::SubArray{<:Any,N,<:AbstractCachedArray}) where N = cacheddata(A)
63+
maybe_cacheddata(A) = A # no-op
64+
6165
convert(::Type{AbstractArray{T}}, S::CachedArray{T}) where T = S
6266
convert(::Type{AbstractArray{T,N}}, S::CachedArray{T,N}) where {T,N} = S
6367
convert(::Type{AbstractArray{T}}, S::CachedArray{<:Any,N}) where {T,N} = convert(AbstractArray{T,N}, S)

src/padded.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,29 @@ function _vcat_resizedata!(::Union{AbstractPaddedLayout, DualLayout{<:PaddedRows
128128
any(iszero,m) || Base.checkbounds(paddeddata(B), m...)
129129
B
130130
end
131+
function _vcat_resizedata!(::Union{DualLayout{<:PaddedRows}, AbstractPaddedLayout}, B::Vcat{<:Any, 1}, m) # ambiguity
132+
iszero(m) || Base.checkbounds(paddeddata(B), m)
133+
B
134+
end
135+
136+
function _vcat_resizedata!(_, B::Vcat{<:Any, 1}, n)
137+
m = n
138+
for arg in arguments(B)
139+
m 0 && break
140+
len = length(arg)
141+
s = min(len, max(m, 0))
142+
resizedata!(arg, s)
143+
m -= s
144+
end
145+
B
146+
end
131147

132148
_vcat_resizedata!(_, B, m...) = B # by default we can't resize
133149

134150
resizedata!(B::Vcat, m...) = _vcat_resizedata!(MemoryLayout(B), B, m...)
135151

152+
cacheddata(B::Vcat) = Vcat(map(maybe_cacheddata, arguments(B))...)
153+
136154
function ==(A::CachedVector{<:Any,<:Any,<:Zeros}, B::CachedVector{<:Any,<:Any,<:Zeros})
137155
length(A) == length(B) || return false
138156
n = max(A.datasize[1], B.datasize[1])

test/cachetests.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using LazyArrays, FillArrays, LinearAlgebra, ArrayLayouts, SparseArrays, Test
44
using StaticArrays
55
import LazyArrays: CachedArray, CachedMatrix, CachedVector, PaddedLayout, CachedLayout, resizedata!, zero!,
66
CachedAbstractArray, CachedAbstractVector, CachedAbstractMatrix, AbstractCachedArray, AbstractCachedMatrix,
7-
PaddedColumns
7+
PaddedColumns, cacheddata, maybe_cacheddata
88

99
using ..InfiniteArrays
1010
using .InfiniteArrays: OneToInf
@@ -487,6 +487,15 @@ using Infinities
487487
B[5, 7] = 3.4
488488
@test A == B
489489
end
490+
491+
@testset "maybe_cacheddata" begin
492+
A = cache(1:10)
493+
@test maybe_cacheddata(A) === cacheddata(A)
494+
B = view(A, 1:5)
495+
@test maybe_cacheddata(B) === cacheddata(B)
496+
C = [1, 2, 3]
497+
@test maybe_cacheddata(C) === C
498+
end
490499
end
491500

492501
end # module

test/concattests.jl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using LazyArrays, FillArrays, LinearAlgebra, ArrayLayouts, Test, Base64
44
using StaticArrays
55
import LazyArrays: MemoryLayout, DenseColumnMajor, materialize!, call, paddeddata,
66
MulAdd, Applied, ApplyLayout, DefaultApplyStyle, sub_materialize, resizedata!,
7-
CachedVector, ApplyLayout, arguments, BroadcastVector, LazyLayout
7+
CachedVector, ApplyLayout, arguments, BroadcastVector, LazyLayout, cacheddata
88

99
@testset "concat" begin
1010
@testset "Vcat" begin
@@ -199,11 +199,38 @@ import LazyArrays: MemoryLayout, DenseColumnMajor, materialize!, call, paddeddat
199199
@test A [1,2,4]
200200
end
201201

202-
@testset "resizedata!" begin
203-
# allow emulating a cached Vector
204-
a = Vcat([1,2], Zeros(8))
205-
@test resizedata!(a, 2) a
206-
@test_throws BoundsError resizedata!(a,3)
202+
@testset "cached vcat" begin
203+
@testset "resizedata!" begin
204+
v = Vcat(1, cache(1:10));
205+
resizedata!(v, 3);
206+
@test v.args[2].datasize == (2,)
207+
v = Vcat(1, [2, 4, 6], cache(7:10), cache(1:5), 50);
208+
resizedata!(v, 1)
209+
@test v.args[3].datasize == v.args[4].datasize == (0,)
210+
resizedata!(v, 5)
211+
@test v.args[3].datasize == (1,)
212+
@test v.args[4].datasize == (0,)
213+
resizedata!(v, 10)
214+
@test v.args[3].datasize == (4,)
215+
@test v.args[4].datasize == (2,)
216+
resizedata!(v, 14)
217+
@test v.args[3].datasize == (4,)
218+
@test v.args[4].datasize == (5,)
219+
resizedata!(v, 50) # test it doesn't break for excess resize
220+
@test v.args[3].datasize == (4,)
221+
@test v.args[4].datasize == (5,)
222+
end
223+
224+
@testset "cacheddata" begin
225+
v = Vcat(1, cache(1:2))
226+
@test @inferred(cacheddata(v)) == [1]
227+
resizedata!(v, 2)
228+
@test @inferred(cacheddata(v)) == [1, 1]
229+
@test cacheddata(v) isa Vcat
230+
end
231+
232+
p = Vcat([1,2], Zeros(4));
233+
# TODO: special behaviour?
207234
end
208235

209236
@testset "Axpy" begin

0 commit comments

Comments
 (0)