Mercurial > repos > public > sbplib_julia
comparison src/Grids/grid.jl @ 1476:62f9d0387a2a feature/grids/componentview
Introduce a view type and implement sliced indexing for the components.
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Fri, 01 Dec 2023 14:58:05 +0100 |
| parents | 76b190ca9a27 |
| children | 06dec7b68f82 553111a15506 |
comparison
equal
deleted
inserted
replaced
| 1475:76b190ca9a27 | 1476:62f9d0387a2a |
|---|---|
| 38 The type of the components of `gf`. | 38 The type of the components of `gf`. |
| 39 """ | 39 """ |
| 40 component_type(T::Type) = eltype(eltype(T)) | 40 component_type(T::Type) = eltype(eltype(T)) |
| 41 component_type(t) = component_type(typeof(t)) | 41 component_type(t) = component_type(typeof(t)) |
| 42 | 42 |
| 43 function componentview(gf, component_index...) | 43 componentview(gf, component_index...) = ArrayComponentView(gf, component_index) |
| 44 A = reinterpret(reshape, component_type(gf), gf) | |
| 45 | 44 |
| 46 component_linear_index = LinearIndices(first(gf))[component_index...] | 45 struct ArrayComponentView{CT,T,D,AT <: AbstractArray{T,D}, IT} <: AbstractArray{CT,D} |
| 47 return @view A[component_linear_index,:,:] | 46 v::AT |
| 47 component_index::IT | |
| 48 | |
| 49 function ArrayComponentView(v, component_index) | |
| 50 CT = typeof(first(v)[component_index...]) | |
| 51 return new{CT, eltype(v), ndims(v), typeof(v), typeof(component_index)}(v,component_index) | |
| 52 end | |
| 48 end | 53 end |
| 54 | |
| 55 Base.size(cv) = size(cv.v) | |
| 56 Base.getindex(cv::ArrayComponentView, i::Int) = cv.v[i][cv.component_index...] | |
| 57 Base.getindex(cv::ArrayComponentView, I::Vararg{Int}) = cv.v[I...][cv.component_index...] | |
| 58 IndexStyle(::Type{<:ArrayComponentView{<:Any,<:Any,AT}}) where AT = IndexStyle(AT) | |
| 59 | |
| 60 # TODO: Implement the remaining optional methods from the array interface | |
| 61 # setindex!(A, v, i::Int) | |
| 62 # setindex!(A, v, I::Vararg{Int, N}) | |
| 63 # iterate | |
| 64 # length(A) | |
| 65 # similar(A) | |
| 66 # similar(A, ::Type{S}) | |
| 67 # similar(A, dims::Dims) | |
| 68 # similar(A, ::Type{S}, dims::Dims) | |
| 69 # # Non-traditional indices | |
| 70 # axes(A) | |
| 71 # similar(A, ::Type{S}, inds) | |
| 72 # similar(T::Union{Type,Function}, inds) | |
| 73 | |
| 74 # TODO: Implement a more general ComponentView that can handle non-AbstractArrays. | |
| 49 | 75 |
| 50 """ | 76 """ |
| 51 refine(g::Grid, r) | 77 refine(g::Grid, r) |
| 52 | 78 |
| 53 The grid where `g` is refined by the factor `r`. | 79 The grid where `g` is refined by the factor `r`. |
