Introduction
ResizingTools
provide some tools to help you resize multi-dimensional arrays with a set of interface methods. Besides, there is a simple implementation of resizable dense array type named SimpleRDArray
as a resizable alternative of Array
.
Get started with SimpleRDArray
SimpleRDArray
is a simple implementation of the resizable dense array, which can be created simply:
julia> using ResizingTools
julia> M = reshape(1:9, 3, 3)
3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64: 1 4 7 2 5 8 3 6 9
julia> RM = SimpleRDArray(M)
3×3 SimpleRDArray{Int64, 2}: 1 4 7 2 5 8 3 6 9
julia> M == RM
true
Once a SimpleRDArray
is created, you can almost do anything with which likes a normal Array
with similar performance:
julia> using BenchmarkTools
julia> @benchmark $RM * $RM
BenchmarkTools.Trial: 10000 samples with 978 evaluations. Range (min … max): 68.202 ns … 4.704 μs ┊ GC (min … max): 0.00% … 0.00% Time (median): 84.563 ns ┊ GC (median): 0.00% Time (mean ± σ): 96.723 ns ± 124.001 ns ┊ GC (mean ± σ): 5.47% ± 5.51% ▁ ▂▅▃▆█▆▆▄▁ ▁▂▂ ▂ █▇▆███████████████▇▆▇▆▅▆▆▆▅▅▅▄▄▁▄▁▃▄▃▅████▆▅▄▄▁▅▆▁▄▄▃▃▃▁▄▃▁▃ █ 68.2 ns Histogram: log(frequency) by time 197 ns < Memory estimate: 160 bytes, allocs estimate: 1.
julia> @benchmark $M * $M
BenchmarkTools.Trial: 10000 samples with 984 evaluations. Range (min … max): 63.316 ns … 11.018 μs ┊ GC (min … max): 0.00% … 0.00% Time (median): 77.442 ns ┊ GC (median): 0.00% Time (mean ± σ): 89.721 ns ± 156.725 ns ┊ GC (mean ± σ): 6.54% ± 5.54% ▅▅▃▇█▇▇▆▃ ▂▁▁ ▂ ▅▅█████████████████▇▇▅▅▄▆▅▆▇▅▆▄▄▅▁▄▄▄▄▄▃▃▁▆█████▇▅▄▄▅▅▅▄▄▃▃▄ █ 63.3 ns Histogram: log(frequency) by time 173 ns < Memory estimate: 160 bytes, allocs estimate: 1.
julia> RM * RM == M * M
true
Besides, a SimpleRDArray
can be resized in many ways:
julia> resize!(RM, (4, 4)) # resize RM to 4 * 4
4×4 SimpleRDArray{Int64, 2}: 1 4 7 81 2 5 8 96 3 6 9 102 4 8 66 126
julia> RM[1:3,1:3] == M
true
julia> resize!(RM, 2, 3) # resize the 2nd dimension of RM to 3
4×3 SimpleRDArray{Int64, 2}: 1 4 7 2 5 8 3 6 9 4 8 66
julia> RM[4, :] .= 0
3-element view(::SimpleRDArray{Int64, 2}, 4, :) with eltype Int64: 0 0 0
julia> resize!(RM, 1, Bool[1, 1, 0, 1]) # delete at index 3 at 1st dimension
3×3 SimpleRDArray{Int64, 2}: 1 4 7 2 5 8 0 0 0