202202A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
203203same type and size `f(size(u))` (defaulting to the same size), which is allocated as
204204needed and then cached within `b` for subsequent usage.
205+
206+ Optionally, the size can be explicitly given at calltime using `b[u,s]`, which will
207+ return a cache of size `s`.
205208"""
206209struct LazyBufferCache{F <: Function }
207210 bufs:: Dict{Any, Any} # a dictionary mapping (type, size) pairs to buffers
@@ -216,15 +219,14 @@ function similar_type(x::AbstractArray{T}, s::NTuple{N, Integer}) where {T, N}
216219 typeof (similar (x, ntuple (Returns (1 ), N)))
217220end
218221
219- function get_tmp (b:: LazyBufferCache , u:: T ) where {T <: AbstractArray }
220- s = b. sizemap (size (u)) # required buffer size
222+ function get_tmp (b:: LazyBufferCache , u:: T , s= b. sizemap (size (u))) where {T <: AbstractArray }
221223 get! (b. bufs, (T, s)) do
222224 similar (u, s) # buffer to allocate if it was not found in b.bufs
223225 end :: similar_type (u, s) # declare type since b.bufs dictionary is untyped
224226end
225227
226228# override the [] method
227- Base. getindex (b:: LazyBufferCache , u:: T ) where {T <: AbstractArray } = get_tmp (b, u)
229+ Base. getindex (b:: LazyBufferCache , u:: T , s = b . sizemap ( size (u))) where {T <: AbstractArray } = get_tmp (b, u, s )
228230
229231# GeneralLazyBufferCache
230232
0 commit comments