Mercurial > repos > public > sbplib_julia
changeset 1270:dcbac783e4c1 refactor/grids
Factor out functions for getting the type and number of components in a type
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 25 Feb 2023 22:42:16 +0100 |
parents | 20f42cf0800c |
children | 5977d7628d55 |
files | src/Grids/grid.jl src/Grids/tensor_grid.jl test/Grids/grid_test.jl |
diffstat | 3 files changed, 30 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/grid.jl Sat Feb 25 22:22:50 2023 +0100 +++ b/src/Grids/grid.jl Sat Feb 25 22:42:16 2023 +0100 @@ -64,3 +64,11 @@ # Should it be lazy? Could it be a view? function getcomponent(gfun, I::Vararg{Int}) end # function getcomponent(gfun, s::Symbol) end ? + + +# TODO: Explain how these are intended to be used +_ncomponents(::Type{<:Number}) = 1 +_ncomponents(T::Type{<:SVector}) = length(T) + +_component_type(T::Type{<:Number}) = T +_component_type(T::Type{<:SVector}) = eltype(T)
--- a/src/Grids/tensor_grid.jl Sat Feb 25 22:22:50 2023 +0100 +++ b/src/Grids/tensor_grid.jl Sat Feb 25 22:42:16 2023 +0100 @@ -78,15 +78,8 @@ function combined_coordinate_vector_type(coordinate_types...) - coord_length(::Type{<:Number}) = 1 - coord_length(T::Type{<:SVector}) = length(T) - - coord_type(T::Type{<:Number}) = T - coord_type(T::Type{<:SVector}) = eltype(T) - - - combined_coord_length = mapreduce(coord_length, +, coordinate_types) - combined_coord_type = mapreduce(coord_type, promote_type, coordinate_types) + combined_coord_length = mapreduce(_ncomponents, +, coordinate_types) + combined_coord_type = mapreduce(_component_type, promote_type, coordinate_types) if combined_coord_length == 1 return combined_coord_type
--- a/test/Grids/grid_test.jl Sat Feb 25 22:22:50 2023 +0100 +++ b/test/Grids/grid_test.jl Sat Feb 25 22:42:16 2023 +0100 @@ -45,3 +45,23 @@ @testset "getcomponent" begin @test_broken false end + +@testset "_ncomponents" begin + @test Grids._ncomponents(Int) == 1 + @test Grids._ncomponents(Float64) == 1 + @test Grids._ncomponents(Rational) == 1 + + @test Grids._ncomponents(SVector{3,Int}) == 3 + @test Grids._ncomponents(SVector{2,Float64}) == 2 + @test Grids._ncomponents(SVector{4,Rational}) == 4 +end + +@testset "_component_type" begin + @test Grids._component_type(Int) == Int + @test Grids._component_type(Float64) == Float64 + @test Grids._component_type(Rational) == Rational + + @test Grids._component_type(SVector{3,Int}) == Int + @test Grids._component_type(SVector{2,Float64}) == Float64 + @test Grids._component_type(SVector{4,Rational}) == Rational +end