From ece736fd6f187a4fe220592508b433d059962f87 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Sun, 14 Apr 2024 02:39:16 +0200 Subject: [PATCH] allow no cache in FixedBasis --- src/bases_fixed.jl | 10 +++++----- src/mtables.jl | 19 +++++++++++-------- test/arithmetic.jl | 10 +++------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/bases_fixed.jl b/src/bases_fixed.jl index 9771300..bfea8c4 100644 --- a/src/bases_fixed.jl +++ b/src/bases_fixed.jl @@ -4,10 +4,10 @@ mutable struct FixedBasis{T,I,V<:AbstractVector{T},M<:MTable{T,I}} <: table::M end -function FixedBasis(basis::AbstractBasis; n::Integer, mt::Integer) - @assert 0 < mt ≤ n +function FixedBasis(basis::AbstractBasis; n::Integer, mt::Integer = 0) + @assert 0 ≤ mt ≤ n elts = Iterators.take(basis, n) - return FixedBasis(elts, mstructure(basis), (mt, mt)) + return FixedBasis(collect(elts), mstructure(basis), (mt, mt)) end function FixedBasis( @@ -15,8 +15,8 @@ function FixedBasis( mstr::MultiplicativeStructure, dims::NTuple{2,I}, ) where {I<:Integer} - @assert 0 < dims[1] ≤ length(elts) - @assert 0 < dims[2] ≤ length(elts) + @assert 0 ≤ dims[1] ≤ length(elts) + @assert 0 ≤ dims[2] ≤ length(elts) @assert !(eltype(elts) <: Integer) return FixedBasis(elts, MTable(elts, mstr, dims)) end diff --git a/src/mtables.jl b/src/mtables.jl index ca275b8..d00c211 100644 --- a/src/mtables.jl +++ b/src/mtables.jl @@ -49,15 +49,18 @@ Base.@propagate_inbounds __iscomputed(mt::MTable, i, j) = isassigned(mt.table, i, j) && !iszero(mt.table[i, j]) Base.@propagate_inbounds function (mt::MTable)(i::Integer, j::Integer) - @boundscheck checkbounds(mt.table, abs(i), abs(j)) - @inbounds begin - i = __absindex(mt, i) - j = __absindex(mt, j) - - if !__iscomputed(mt, i, j) - complete!(mt, i, j) + i = __absindex(mt, i) + j = __absindex(mt, j) + if checkbounds(Bool, mt.table, i, j) + @inbounds begin + if !__iscomputed(mt, i, j) + complete!(mt, i, j) + end + return mt.table[i, j] end - return mt.table[i, j] + else + g, h = mt[i], mt[j] + return mt.mstr(g, h) end end Base.@propagate_inbounds function (mt::MTable{T})(x::T, y::T) where {T} diff --git a/test/arithmetic.jl b/test/arithmetic.jl index 1b46400..f22fd7c 100644 --- a/test/arithmetic.jl +++ b/test/arithmetic.jl @@ -146,12 +146,8 @@ end RG = StarAlgebra(A★, B) @test basis(RG) === B - words = collect(Iterators.take(A★, nwords(A★, 0, 8))) - l = nwords(A★, 4) - - @assert length(words[l]) == 4 && length(words[l+1]) == 5 - - fB = SA.FixedBasis(words, SA.DiracMStructure(*), UInt32.((l, l))) + # no caching + fB = SA.FixedBasis(basis(RG); n = nwords(A★, 0, 8), mt = 0) @test fB.table.elts === fB.elts fRG = StarAlgebra(A★, fB) @@ -279,7 +275,7 @@ end end @testset "FixedBasis caching && allocations" begin - fB = SA.FixedBasis(words, SA.DiracMStructure(*), UInt32.((l, l))) + fB = SA.FixedBasis(B; n = nwords(A★, 8), mt = UInt32(nwords(A★, 4))) fRG = StarAlgebra(A★, fB) k = size(SA.mstructure(basis(fRG)), 1)