Vec.jl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #Adapted from IndirectArray
  2. struct Vec{T, N} <: AbstractArray{T,1}
  3. cpp_object
  4. data::AbstractArray{T, 1}
  5. cpp_allocated::Bool
  6. @inline function Vec{T, N}(obj) where {T, N}
  7. new{T, N}(obj, Base.unsafe_wrap(Array{T, 1}, Ptr{T}(obj.cpp_object), N), true)
  8. end
  9. @inline function Vec{T, N}(data_raw::AbstractArray{T, 1}) where {T, N}
  10. if size(data_raw, 1) != N
  11. throw("Array is improper Size for Vec declared")
  12. end
  13. new{T, N}(nothing, data_raw, false)
  14. end
  15. end
  16. function Base.deepcopy_internal(x::Vec{T,N}, y::IdDict) where {T, N}
  17. if haskey(y, x)
  18. return y[x]
  19. end
  20. ret = Base.copy(x)
  21. y[x] = ret
  22. return ret
  23. end
  24. Base.size(A::Vec) = Base.size(A.data)
  25. Base.axes(A::Vec) = Base.axes(A.data)
  26. Base.IndexStyle(::Type{Vec{T,N}}) where {T, N} = IndexLinear()
  27. Base.strides(A::Vec{T,N}) where {T, N} = (1)
  28. function Base.copy(A::Vec{T,N}) where {T, N}
  29. return Vec{T, N}(copy(A.data))
  30. end
  31. Base.pointer(A::Vec) = Base.pointer(A.data)
  32. Base.unsafe_convert(::Type{Ptr{T}}, A::Vec{S, N}) where {T, S, N} = Base.unsafe_convert(Ptr{T}, A.data)
  33. @inline function Base.getindex(A::Vec{T,N}, I::Int) where {T, N}
  34. @boundscheck checkbounds(A.data, I)
  35. return A.data[I]
  36. end
  37. @inline function Base.setindex!(A::Vec, x, I::Int)
  38. @boundscheck checkbounds(A.data, I)
  39. A.data[I] = x
  40. return A
  41. end