Mat.jl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #Adapted from IndirectArray
  2. struct Mat{T <: dtypes} <: AbstractArray{T,3}
  3. mat
  4. data_raw
  5. data
  6. @inline function Mat{T}(mat, data_raw::AbstractArray{T,3}) where {T <: dtypes}
  7. data = reinterpret(T, data_raw)
  8. new{T}(mat, data_raw, data)
  9. end
  10. @inline function Mat(data_raw::AbstractArray{T, 3}) where {T <: dtypes}
  11. data = reinterpret(T, data_raw)
  12. mat = nothing
  13. new{T}(mat, data_raw, data)
  14. end
  15. end
  16. function Base.deepcopy_internal(x::Mat{T}, y::IdDict) where {T}
  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::Mat) = size(A.data)
  25. Base.axes(A::Mat) = axes(A.data)
  26. Base.IndexStyle(::Type{Mat{T}}) where {T} = IndexCartesian()
  27. Base.strides(A::Mat{T}) where {T} = strides(A.data)
  28. Base.copy(A::Mat{T}) where {T} = Mat(copy(A.data_raw))
  29. Base.pointer(A::Mat) = Base.pointer(A.data)
  30. Base.unsafe_convert(::Type{Ptr{T}}, A::Mat{S}) where {T, S} = Base.unsafe_convert(Ptr{T}, A.data)
  31. @inline function Base.getindex(A::Mat{T}, I::Vararg{Int,3}) where {T}
  32. @boundscheck checkbounds(A.data, I...)
  33. @inbounds ret = A.data[I...]
  34. ret
  35. end
  36. @inline function Base.setindex!(A::Mat, x, I::Vararg{Int,3})
  37. @boundscheck checkbounds(A.data, I...)
  38. A.data[I...] = x
  39. return A
  40. end