From 4db6564a1b26e30890ddd3cf8bae2a7355010188 Mon Sep 17 00:00:00 2001 From: guoyongzhi Date: Wed, 6 Nov 2024 17:55:18 +0800 Subject: [PATCH] refactor(datatypes): define SVector4 --- src/common_datatypes.jl | 28 +++++++++++++++++++++++++++- src/qtree_functions.jl | 19 ++----------------- src/qtrees.jl | 4 ++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/common_datatypes.jl b/src/common_datatypes.jl index 36d5936..ed3da0b 100644 --- a/src/common_datatypes.jl +++ b/src/common_datatypes.jl @@ -1,5 +1,5 @@ module CommonDatatypes -export DoubleList, ListNode, IntMap, movetofirst!, next, prev, value, setvalue!, ishead, istail, seek_head, seek_tail +export DoubleList, ListNode, IntMap, SVector4, movetofirst!, next, prev, value, setvalue!, ishead, istail, seek_head, seek_tail mutable struct ListNode{T} value::T prev::ListNode{T} @@ -144,4 +144,30 @@ end Base.haskey(im::IntMap, key) = isassigned(im.map, key) Base.getindex(im::IntMap, ind...) = getindex(im.map, ind...) Base.setindex!(im::IntMap, v, ind...) = setindex!(im.map, v, ind...) + +mutable struct SVector4{T} + e1::T + e2::T + e3::T + e4::T + len::Int + SVector4{T}() where T = (v = new(); v.len = 0; v) +end +function SVector4{T}(e1, e2, e3, e4) where T + v = SVector4{T}() + v.e1 = e1 + v.e2 = e2 + v.e3 = e3 + v.e4 = e4 + v.len = 4 + v +end +Base.getindex(v::SVector4, i) = getfield(v, i) +Base.setindex!(v::SVector4, x, i) = setfield!(v, i, x) +Base.push!(v::SVector4, x) = v[v.len += 1] = x +Base.length(v::SVector4) = v.len +Base.iterate(v::SVector4, i=1) = i <= length(v) ? (v[i], i+1) : nothing +Base.eltype(::Type{SVector4{T}}) where T = T + + end diff --git a/src/qtree_functions.jl b/src/qtree_functions.jl index ae78ab5..5fcb2c8 100644 --- a/src/qtree_functions.jl +++ b/src/qtree_functions.jl @@ -141,21 +141,6 @@ end hash_spacial_qtree() = HashSpacialQTree() hash_spacial_qtree(qts) = hash_spacial_qtree() -mutable struct Indices4 - i1::Index - i2::Index - i3::Index - i4::Index - len::Int - Indices4() = (ii = new(); ii.len = 0; ii) -end -Base.getindex(ii::Indices4, i) = getfield(ii, i) -Base.setindex!(ii::Indices4, x, i) = setfield!(ii, i, x) -Base.push!(ii::Indices4, x) = ii[ii.len += 1] = x -Base.length(ii::Indices4) = ii.len -Base.iterate(ii::Indices4, i=1) = i <= length(ii) ? (ii[i], i+1) : nothing -Base.eltype(::Type{Indices4}) = Index - function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, LinkedSpacialQTree}, label::Int) l = length(qt) #l always >= 2 # @assert kernelsize(qt[l], 1) <= 2 && kernelsize(qt[l], 2) <= 2 @@ -171,7 +156,7 @@ function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, Link # @assert l >= 2 @inbounds mat = qt[l] rs, cs = getshift(mat) - inds = Indices4() + inds = SVector4{Index}() @inbounds mat[rs+1, cs+1] != EMPTY && push!(inds, (l, rs+1, cs+1)) @inbounds mat[rs+1, cs+2] != EMPTY && push!(inds, (l, rs+1, cs+2)) @inbounds mat[rs+2, cs+1] != EMPTY && push!(inds, (l, rs+2, cs+1)) @@ -180,7 +165,7 @@ function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, Link l = l + 1 @inbounds mat = qt[l] rs, cs = getshift(mat) - inds2 = Indices4() + inds2 = SVector4{Index}() @inbounds mat[rs+1, cs+1] != EMPTY && push!(inds2, (l, rs+1, cs+1)) @inbounds mat[rs+1, cs+2] != EMPTY && push!(inds2, (l, rs+1, cs+2)) @inbounds mat[rs+2, cs+1] != EMPTY && push!(inds2, (l, rs+2, cs+1)) diff --git a/src/qtrees.jl b/src/qtrees.jl index 05c7cd6..33d5f9e 100644 --- a/src/qtrees.jl +++ b/src/qtrees.jl @@ -339,11 +339,11 @@ end mutable struct QTreeNode{T} value::T parent::QTreeNode{T} - children::Vector{QTreeNode{T}} + children::SVector4{QTreeNode{T}} function QTreeNode{T}() where T n = new{T}() n.parent = n - n.children = [n, n, n, n] + n.children = SVector4{QTreeNode{T}}(n, n, n, n) n end QTreeNode{T}(v, p, c) where T = new{T}(v, p, c)