Skip to content

Commit

Permalink
refactor(datatypes): define SVector4
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Nov 6, 2024
1 parent 71f7bf4 commit 4db6564
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
28 changes: 27 additions & 1 deletion src/common_datatypes.jl
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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
19 changes: 2 additions & 17 deletions src/qtree_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/qtrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4db6564

Please sign in to comment.