From a13b103e826ed1a5a91c9209312add6d8a591e95 Mon Sep 17 00:00:00 2001 From: singularitti Date: Sat, 7 Oct 2023 20:38:27 -0400 Subject: [PATCH] Add constructors of `Indices`, use constructors to implement `convert` See https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Defining-New-Conversions --- src/miller.jl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/miller.jl b/src/miller.jl index 23764da..decc5dc 100644 --- a/src/miller.jl +++ b/src/miller.jl @@ -152,16 +152,23 @@ Base.IndexStyle(::Type{<:Indices}) = IndexLinear() Base.getindex(x::Indices, i::Int) = getindex(x.data, i) -Base.convert(::Type{T}, x::T) where {T<:Indices} = x -Base.convert(::Type{Miller}, mb::MillerBravais) = - Miller(2 * mb[1] + mb[2], 2 * mb[2] + mb[1], mb[4]) -Base.convert(::Type{ReciprocalMiller}, mb::ReciprocalMillerBravais) = - ReciprocalMiller(mb[1], mb[2], mb[4]) -Base.convert(::Type{MillerBravais}, m::Miller) = +Miller(mb::MillerBravais) = Miller(2 * mb[1] + mb[2], 2 * mb[2] + mb[1], mb[4]) + +ReciprocalMiller(mb::ReciprocalMillerBravais) = ReciprocalMiller(mb[1], mb[2], mb[4]) + +MillerBravais(m::Miller) = MillerBravais(2 * m[1] - m[2], 2 * m[2] - m[1], -(m[1] + m[2]), 3 * m[3]) -Base.convert(::Type{ReciprocalMillerBravais}, m::ReciprocalMiller) = + +ReciprocalMillerBravais(m::ReciprocalMiller) = ReciprocalMillerBravais(m[1], m[2], -(m[1] + m[2]), m[3]) +Base.convert(::Type{T}, x::T) where {T<:Indices} = x +Base.convert(::Type{Miller}, mb::MillerBravais) = Miller(mb) +Base.convert(::Type{ReciprocalMiller}, mb::ReciprocalMillerBravais) = ReciprocalMiller(mb) +Base.convert(::Type{MillerBravais}, m::Miller) = MillerBravais(m) +Base.convert(::Type{ReciprocalMillerBravais}, m::ReciprocalMiller) = + ReciprocalMillerBravais(m) + Base.show(io::IO, x::Union{Miller,MillerBravais}) = print(io, '<', join(x.data, " "), '>') Base.show(io::IO, x::Union{ReciprocalMiller,ReciprocalMillerBravais}) = print(io, '{', join(x.data, " "), '}')