comparison src/Grids/grid.jl @ 1496:ae2dbfb984a9 feature/grids/curvilinear

Merge feature/grids/componentview
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 01 Dec 2023 15:01:44 +0100
parents 62f9d0387a2a
children 06dec7b68f82 553111a15506
comparison
equal deleted inserted replaced
1495:64b58740e029 1496:ae2dbfb984a9
31 """ 31 """
32 coordinate_size(::Type{<:Grid{T}}) where T = _ncomponents(T) 32 coordinate_size(::Type{<:Grid{T}}) where T = _ncomponents(T)
33 coordinate_size(g::Grid) = coordinate_size(typeof(g)) # TBD: Name of this function?! 33 coordinate_size(g::Grid) = coordinate_size(typeof(g)) # TBD: Name of this function?!
34 34
35 """ 35 """
36 component_type(g) 36 component_type(gf)
37 37
38 The type of the components of the coordinate vector of `Grid` `g`. 38 The type of the components of `gf`.
39 """ 39 """
40 component_type(::Type{<:Grid{T}}) where T = eltype(T) 40 component_type(T::Type) = eltype(eltype(T))
41 component_type(g::Grid) = component_type(typeof(g)) 41 component_type(t) = component_type(typeof(t))
42
43 componentview(gf, component_index...) = ArrayComponentView(gf, component_index)
44
45 struct ArrayComponentView{CT,T,D,AT <: AbstractArray{T,D}, IT} <: AbstractArray{CT,D}
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
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.
42 75
43 """ 76 """
44 refine(g::Grid, r) 77 refine(g::Grid, r)
45 78
46 The grid where `g` is refined by the factor `r`. 79 The grid where `g` is refined by the factor `r`.