From 41a8352cffba9766d2116f6d1129a7067508b741 Mon Sep 17 00:00:00 2001 From: "mhsatman@gmail.com" Date: Thu, 7 Mar 2024 18:32:26 +0300 Subject: [PATCH] replace some eltype with genetic typing, use isone and iszero for type stable code --- src/functions.jl | 66 +++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/src/functions.jl b/src/functions.jl index 04f0832..76b93cf 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -134,13 +134,13 @@ represent ordered data representing a curve. function interp_biharmonic_spline(x::Union{Vector{T}, AbstractRange{T}},y::Union{Vector{T}, AbstractRange{T}},xi::Union{Vector{T}, AbstractRange{T}}; extrapolate_method=:linear,pad_data=:linear) where T<:Real # Pad data if needed - if isa(x,AbstractRange{eltype(x)}) + if isa(x,AbstractRange{T}) xx = collect(x) else xx = deepcopy(x) end - if isa(y,AbstractRange{eltype(y)}) + if isa(y,AbstractRange{T}) yy = collect(y) else yy = deepcopy(y) @@ -179,7 +179,7 @@ function interp_biharmonic_spline(x::Union{Vector{T}, AbstractRange{T}},y::Union elseif pad_data==:none # No padding else - error(""" Invalid pad_data method provided, valued options are: linear, constant, and none """) + error("Invalid pad_data method provided, valued options are: linear, constant, and none") end # Change behaviour depending on extrapolation method @@ -214,12 +214,13 @@ function interp_biharmonic_spline(x::Union{Vector{T}, AbstractRange{T}},y::Union # Allow extrapolation as per the biharmonic function yi = interp_biharmonic(xx,yy,xi) else - error(""" Invalid extrapolate_method method provided, valued options are: linear, constant, and biharmonic """) + error("Invalid extrapolate_method method provided, valued options are: linear, constant, and biharmonic") end return yi end + """ interp_biharmonic(x,y,xi) @@ -300,12 +301,13 @@ end function lerp_(x,y,xi) j = findfirst(x.>xi) - if j==1 # Deal with extrapolation at the start - i=1 - j=2 - elseif isnothing(j) # Deal with extrapolation at the end + + if isnothing(j) # Deal with extrapolation at the end j = length(x) i = j-1 + elseif isone(j) # Deal with extrapolation at the start + i=1 + j=2 else i = j-1 end @@ -369,10 +371,10 @@ function mindist(V1,V2; getIndex=false, skipSelf = false ) end end -function unique_dict_index(X; sort_entries=false) +function unique_dict_index(X::AbstractVector{T}; sort_entries=false) where T<:Real # Here a normal Dict is used to keep track of unique elements. Normal dicts do not maintain element insertion order. # Hence the unique indices need to seperately be tracked. - T = eltype(X) + # T = eltype(X) d = Dict{T,Nothing}() # Use dict to keep track of used values xUni = Vector{T}() indUnique = Vector{Int64}() @@ -419,10 +421,10 @@ function unique_dict_index_inverse(X; sort_entries=false) return xUni, indUnique, indInverse end -function unique_dict_index_count(X; sort_entries=false) +function unique_dict_index_count(X::AbstractVector{T}; sort_entries=false) where T<:Real # Here a normal Dict is used to keep track of unique elements. Normal dicts do not maintain element insertion order. # Hence the unique indices need to seperately be tracked. - T = eltype(X) + # T = eltype(X) d = Dict{T,Int64}() # Use dict to keep track of used values xUni = Vector{T}() indUnique = Vector{Int64}() @@ -448,10 +450,10 @@ function unique_dict_index_count(X; sort_entries=false) return xUni, indUnique, c end -function unique_dict_index_inverse_count(X; sort_entries=false) +function unique_dict_index_inverse_count(X::AbstractVector{T}; sort_entries=false) where T <: Real # Here a normal Dict is used to keep track of unique elements. Normal dicts do not maintain element insertion order. # Hence the unique indices need to seperately be tracked. - T = eltype(X) + # T = eltype(X) d = Dict{T,Int64}() # Use dict to keep track of used values xUni = Vector{T}() indUnique = Vector{Int64}() @@ -480,10 +482,10 @@ function unique_dict_index_inverse_count(X; sort_entries=false) return xUni, indUnique, indInverse,c end -function unique_dict_count(X; sort_entries=false) +function unique_dict_count(X::AbstractVector{T}; sort_entries=false) where T <: Real # Here a normal Dict is used to keep track of unique elements. Normal dicts do not maintain element insertion order. # Hence the unique indices need to seperately be tracked. - T = eltype(X) + # T = eltype(X) d = Dict{T,Int64}() # Use dict to keep track of used values xUni = Vector{T}() c = Vector{Int64}() @@ -506,10 +508,10 @@ function unique_dict_count(X; sort_entries=false) return xUni, c end -function unique_dict_inverse(X; sort_entries=false) +function unique_dict_inverse(X::AbstractVector{T}; sort_entries=false) where T <: Real # Here a normal Dict is used to keep track of unique elements. Normal dicts do not maintain element insertion order. # Hence the unique indices need to seperately be tracked. - T = eltype(X) + # T = eltype(X) d = Dict{T,Int64}() # Use dict to keep track of used values xUni = Vector{T}() indInverse = Vector{Int64}(undef,length(X)) @@ -597,7 +599,7 @@ function ind2sub_(ind,numDim,k) a = Vector{Int64}(undef,numDim) # Initialise a for q ∈ numDim:-1:1 # For all dimensions - if q==1 # First 1st dimension + if isone(q) # First 1st dimension a[1] = rem(ind-1,k[1]) + 1 else p = rem(ind-1,k[q-1]) + 1 # "previous" @@ -996,7 +998,7 @@ r::Float64, defining circumsphere radius """ function platonicsolid(n::Integer,r=1.0) - if n==1 + if isone(n) M = tetrahedron(r) elseif n==2 M = cube(r) @@ -1010,8 +1012,8 @@ function platonicsolid(n::Integer,r=1.0) return M end -function unique_dict(X) - d = OrderedDict{eltype(X),Int64}() # Use dict to keep track of used values +function unique_dict(X::AbstractVector{T}) where T <: Real + d = OrderedDict{T ,Int64}() # Use dict to keep track of used values indUnique = Vector{Int64}() indReverse = Vector{Int64}(undef,length(X)) # c = Vector{Int64}() @@ -1044,9 +1046,9 @@ end function subtri(F,V,n; method = :linear) - if n==0 + if iszero(n) return F,V - elseif n==1 + elseif isone(n) E = meshedges(F) Eu,_,indReverse = gunique(E; return_unique=true, return_index=true, return_inverse=true, sort_entries=true) @@ -1121,9 +1123,9 @@ function subtri(F,V,n; method = :linear) end function subquad(F,V,n; method=:linear) - if n==0 + if iszero(n) return F,V - elseif n==1 + elseif isone(n) # Get edges E = meshedges(F) # Non-unique edges @@ -1264,7 +1266,7 @@ function hexbox(boxDim,boxEl) end F_uni,indUni,c_uni = gunique(F,return_index=true, return_inverse=false,return_counts=true,sort_entries=true) - Lb = c_uni.==1 + Lb = isone.(c_uni) Fb = F_uni[Lb] CF_type_uni = CF_type[indUni] CFb_type = CF_type_uni[Lb] @@ -1922,7 +1924,7 @@ function trisurfslice(F,V,n = (0.0,0.0,1.0), p = mean(V,dims=1); snapTolerance = end else # Some below -> cut nBelow = sum(lf) # Number of nodes below - if nBelow==1 # 2-above, 1 below + if isone(nBelow) # 2-above, 1 below indP = f[wrapindex(findfirst(lf) .+ (0:2),3)] e1 = sort(indP[[1,2]]) @@ -1996,7 +1998,7 @@ function boundaryedges(F) E = meshedges(F) Eu,_,indReverse = gunique(E; return_unique=true, return_index=true, return_inverse=true, sort_entries=true) count_E2F = count_edge_face(F,Eu,indReverse) - return Eu[count_E2F.==1] + return Eu[isone.(count_E2F)] end function edges2curve(Eb) @@ -2038,11 +2040,11 @@ function extrudecurve(V1,d; s=1, n=Point{3, Float64}(0.0,0.0,1.0),num_steps=miss num_steps = num_steps + Int64(iseven(num_steps)) # Force uneven end end - if s==1 # Allong n from V1 + if isone(s) # Allong n from V1 p = d.*n elseif s==-1 # Against n from V1 p = -d.*n - elseif s==0 # Extrude both ways from V1 + elseif iszero(s) # Extrude both ways from V1 p = d.*n V1 = [(eltype(V1))(v.-p./2) for v ∈ V1] #Shift V1 in negative direction end @@ -2100,7 +2102,7 @@ function meshgroup(F; con_type = :v) end if np == length(seen) # Group full c += 1 # Increment group counter - i = findfirst(C.==0) + i = findfirst(iszero.(C)) end end end