JuliaArrays / ArrayViews.jl

A Julia package to explore a new system of array views
MIT License
19 stars 18 forks source link

Deepcopy Behavior #31

Closed JaredCrean2 closed 9 years ago

JaredCrean2 commented 9 years ago

Deepcopying an ArrayView gives another ArrayView, so the underlying data is still shared. Would it be more consistent with the idea of deepcopying to return a new, fully independent array with the values copied?

Here is a simple test case:

using ArrayViews

A = rand(3,3)
b = view(A, :, 1)
c = deepcopy(b)
println("typeof(c) = ", typeof(c))

gives output typeof(c) = ArrayViews.ContiguousView{Float64,1,Array{Float64,2}}

kmsquire commented 9 years ago

I don't see that the underlying data is still shared:

julia> b[1] = 1.0
1.0

julia> A
3x3 Array{Float64,2}:
 1.0       0.670383  0.353127
 0.440465  0.703311  0.0540496
 0.126488  0.63794   0.122775

julia> b
3-element ArrayViews.ContiguousView{Float64,1,Array{Float64,2}}:
 1.0
 0.440465
 0.126488

julia> c
3-element ArrayViews.ContiguousView{Float64,1,Array{Float64,2}}:
 0.758112
 0.440465
 0.126488

It actually seems that the underlying array was copied:

julia> c[1] = 2.0
2.0

julia> b
3-element ArrayViews.ContiguousView{Float64,1,Array{Float64,2}}:
 1.0
 0.440465
 0.126488

julia> c.arr
3x3 Array{Float64,2}:
 2.0       0.670383  0.353127
 0.440465  0.703311  0.0540496
 0.126488  0.63794   0.122775

So, the array is independent, and it's a full copy of b (including the underlying array). Is this okay? If you want a simple matrix, you could just use copy.

JaredCrean2 commented 9 years ago

Ah, yes, copy is what I am looking for. I did not realize the entire underlying array was copied using deepcopy.