Mercurial > repos > public > sbplib_julia
diff src/Grids/grid.jl @ 1524:b2b9693aa63b
Merge feature/grids/componentview
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 22 Mar 2024 08:59:36 +0100 |
parents | 2fa325f08b66 |
children | 4df668d00d03 d641798539c2 |
line wrap: on
line diff
--- a/src/Grids/grid.jl Fri Mar 22 08:48:00 2024 +0100 +++ b/src/Grids/grid.jl Fri Mar 22 08:59:36 2024 +0100 @@ -49,6 +49,40 @@ component_type(t) = component_type(typeof(t)) """ + componentview(gf, component_index...) + +A view of `gf` with only the components specified by `component_index...`. + +# Examples +```julia-repl +julia> componentview([[1,2], [2,3], [3,4]],2) +3-element ArrayComponentView{Int64, Vector{Int64}, 1, Vector{Vector{Int64}}, Tuple{Int64}}: + 2 + 3 + 4 +``` +""" +componentview(gf, component_index...) = ArrayComponentView(gf, component_index) + +struct ArrayComponentView{CT,T,D,AT <: AbstractArray{T,D}, IT} <: AbstractArray{CT,D} + v::AT + component_index::IT + + function ArrayComponentView(v, component_index) + CT = typeof(first(v)[component_index...]) + return new{CT, eltype(v), ndims(v), typeof(v), typeof(component_index)}(v,component_index) + end +end + +Base.size(cv) = size(cv.v) +Base.getindex(cv::ArrayComponentView, i::Int) = cv.v[i][cv.component_index...] +Base.getindex(cv::ArrayComponentView, I::Vararg{Int}) = cv.v[I...][cv.component_index...] +IndexStyle(::Type{<:ArrayComponentView{<:Any,<:Any,AT}}) where AT = IndexStyle(AT) + +# TODO: Implement `setindex!`? +# TODO: Implement a more general ComponentView that can handle non-AbstractArrays. + +""" refine(g::Grid, r) The grid where `g` is refined by the factor `r`.