AcceleratedArrays.jl icon indicating copy to clipboard operation
AcceleratedArrays.jl copied to clipboard

Implement copy

Open chris-b1 opened this issue 4 years ago • 2 comments

julia> a = accelerate([1, 2, 3], UniqueSortIndex)
3-element Array{Int64,1} + UniqueSortIndex:
 1
 2
 3

julia> copy(a)
3-element Array{Int64,1}:
 1
 2
 3

Unless I'm missing some subtlety where this could create an invalid state, this seems to work?

Base.copy(a::AcceleratedArray{T, N, A, I}) where {T,N,A,I} = AcceleratedArray{T, N, A, I}(copy(a.parent), a.index)

chris-b1 avatar May 07 '20 15:05 chris-b1

That should be safe.

I haven't thought a lot about this - but copy is only useful if you want to mutate something, and currenty you can't safely mutate an AcceleratedArray (obviously we'd like to support that though). So we'd want to implement that first in any case, otherwise we might be leading people astray.

andyferris avatar May 21 '20 06:05 andyferris

Thanks, my motivating example is storing an AcceleratedArray in a DataFrame. For that, you don't necessarily need the underlying mutability, but do need a copy to prevent the index for being dropped on operations that copy the dataframe. E.g.

julia> df = DataFrame!(a=accelerate([1, 2, 3], UniqueSortIndex), b=[4, 5, 6])
3×2 DataFrame
│ Row │ a     │ b     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 4     │
│ 2   │ 2     │ 5     │
│ 3   │ 3     │ 6     │

julia> df.a
3-element Array{Int64,1} + UniqueSortIndex:
 1
 2
 3

julia> new = transform(df, :a => ByRow(isequal(1)) => :c)
3×3 DataFrame
│ Row │ a     │ b     │ c    │
│     │ Int64 │ Int64 │ Bool │
├─────┼───────┼───────┼──────┤
│ 1   │ 1     │ 4     │ 1    │
│ 2   │ 2     │ 5     │ 0    │
│ 3   │ 3     │ 6     │ 0    │

julia> new.a
3-element Array{Int64,1}:
 1
 2
 3

chris-b1 avatar May 21 '20 11:05 chris-b1