1#Adapted from IndirectArray
2
3struct Mat{T <: dtypes} <: AbstractArray{T,3}
4    mat
5    data_raw
6    data
7
8    @inline function Mat{T}(mat, data_raw::AbstractArray{T,3}) where {T <: dtypes}
9        data = reinterpret(T, data_raw)
10        new{T}(mat, data_raw, data)
11    end
12
13    @inline function Mat(data_raw::AbstractArray{T, 3}) where {T <: dtypes}
14        data = reinterpret(T, data_raw)
15        mat = nothing
16        new{T}(mat, data_raw, data)
17    end
18end
19
20function Base.deepcopy_internal(x::Mat{T}, y::IdDict) where {T}
21    if haskey(y, x)
22        return y[x]
23    end
24    ret = Base.copy(x)
25    y[x] = ret
26    return ret
27end
28
29Base.size(A::Mat) = size(A.data)
30Base.axes(A::Mat) = axes(A.data)
31Base.IndexStyle(::Type{Mat{T}}) where {T} = IndexCartesian()
32
33Base.strides(A::Mat{T}) where {T} = strides(A.data)
34Base.copy(A::Mat{T}) where {T} = Mat(copy(A.data_raw))
35Base.pointer(A::Mat) = Base.pointer(A.data)
36
37Base.unsafe_convert(::Type{Ptr{T}}, A::Mat{S}) where {T, S} = Base.unsafe_convert(Ptr{T}, A.data)
38
39@inline function Base.getindex(A::Mat{T}, I::Vararg{Int,3}) where {T}
40    @boundscheck checkbounds(A.data, I...)
41    @inbounds ret = A.data[I...]
42    ret
43end
44
45@inline function Base.setindex!(A::Mat, x, I::Vararg{Int,3})
46    @boundscheck checkbounds(A.data, I...)
47    A.data[I...] = x
48    return A
49end
50