Skip to content

Commit

Permalink
improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Nov 11, 2024
1 parent 9caf017 commit 3a2ff83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/common_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function SVector4{T}(e1, e2, e3, e4) where T
v.len = 4
v
end
SVector4(e1::T, e2::T, e3::T, e4::T) where T = SVector4{T}(e1, e2, e3, e4)
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
Expand Down
6 changes: 3 additions & 3 deletions src/qtree_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ function partialcollisions(qtrees::AbstractVector,
append!(itemlist, (((label, lb) => spindex) for lb in next(listnode)))
tn = treenode
while !isroot(tn)
tn = tn.parent #root不是哨兵,值需要遍历
tn = parent(tn) #root不是哨兵,值需要遍历
if !isemptynodelist(tn)
plbs = Iterators.filter(!in(labels), getnodelist(tn)) #move了的plb不加入,等候其向下遍历时加,避免重复
collisions_boundsfilter(qtrees, spindex, label, plbs, itemlist, colist)
end
end
empty!(treenodestack)
for c in treenode.children
for c in children(treenode)
isemptychild(treenode, c) || push!(treenodestack, c)
end
while !isempty(treenodestack)
Expand All @@ -313,7 +313,7 @@ function partialcollisions(qtrees::AbstractVector,
# @show cspindex clbs
collisions_boundsfilter(qtrees, cspindex, clbs, label, itemlist, colist)
end
for c in tn.children
for c in children(tn)
if !isemptychild(tn, c) #如果isemptychild则该child无意义
emptyflag = false
push!(treenodestack, c)
Expand Down
32 changes: 19 additions & 13 deletions src/qtrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mutable struct QTreeNode{T}
function QTreeNode{T}() where T
n = new{T}()
n.parent = n
n.children = SVector4{QTreeNode{T}}(n, n, n, n)
n.children = SVector4(n, n, n, n)
n
end
QTreeNode{T}(v, p, c) where T = new{T}(v, p, c)
Expand All @@ -374,19 +374,25 @@ end
QTreeNode(value::T) where T = QTreeNode{T}(value)
QTreeNode(value::T, parent, children) where T = QTreeNode{T}(value, parent, children)
QTreeNode(parent::QTreeNode{T}, args...) where T = QTreeNode{T}(parent, args...)
isroot(n::QTreeNode) = n === n.parent
parent(n::QTreeNode) = n.parent
setparent!(n::QTreeNode, p::QTreeNode) = n.parent = p
children(n::QTreeNode) = n.children
Base.@propagate_inbounds child(n::QTreeNode, i) = n.children[i]
Base.@propagate_inbounds setchild!(n::QTreeNode, c::QTreeNode, i) = n.children[i] = c
isroot(n::QTreeNode) = n === parent(n)
isemptychild(n::QTreeNode, c::QTreeNode) = n === c
function Base.iterate(n::QTreeNode, Q=[n])
isempty(Q) && return nothing
n = popfirst!(Q)
for c in n.children
for c in children(n)
(!isemptychild(n, c)) && push!(Q, c)
end
return n, Q
end

Base.eltype(::Type{QTreeNode{T}}) where T = QTreeNode{T}
CommonDatatypes.next(n::QTreeNode) = n.parent # use as a linked list node
CommonDatatypes.setnext!(n::QTreeNode, next::QTreeNode) = n.parent = next
CommonDatatypes.next(n::QTreeNode) = parent(n) # use as a linked list node
CommonDatatypes.setnext!(n::QTreeNode, next::QTreeNode) = setparent!(n, next)

################ HashSpacialQTree
abstract type AbstractSpacialQTree end
Expand Down Expand Up @@ -478,16 +484,16 @@ function new_spacial_qtree_node(t::LinkedSpacialQTree, parent::SpacialQTreeNode,
# @show length(cache)
n = popfirst!(cache)
# @assert n.children == [n,n,n,n]
n.parent = parent
setparent!(n, parent)
n.value.index = index
return n
end
end
function remove_tree_node(t::LinkedSpacialQTree, node::SpacialQTreeNode)
p = node.parent
p = parent(node)
ind = spacialindex(node)
# @show ind
p.children[childnumber(ind)+1] = p
setchild!(p, p, childnumber(ind)+1)
pushfirst!(t.treenode_pool, node)
end
function Base.push!(t::LinkedSpacialQTree, ind::Index, label::Int)
Expand All @@ -501,10 +507,10 @@ function Base.push!(t::LinkedSpacialQTree, ind::Index, label::Int)
aind[1] <= ind[1] && break
cn = childnumber(aind, ind)
# @show aind, ind, cn
cnode = tn.children[cn+1]
cnode = child(tn, cn+1)
if isemptychild(tn, cnode)
cnode = new_spacial_qtree_node(t, tn, child(aind, cn))
tn.children[cn+1] = cnode
setchild!(tn, cnode, cn+1)
end
tn = cnode
end
Expand All @@ -520,12 +526,12 @@ function seektreenode(listnode::ListNode{Int})
unsafe_pointer_to_objref(Ptr{Any}(value(head)))::SpacialQTreeNode
end
function clear!(t::LinkedSpacialQTree, label::Int)
if haskey(t.label_to_nodes, label) && !isempty(t.label_to_nodes[label])
nodes = t.label_to_nodes[label]
if !isemptylabelnodes(t, label)
nodes = getlabelnodes(t, label)
for n in nodes
pushfirst!(t.listnode_pool, pop!(n))
end
empty!(t.label_to_nodes[label])
empty!(nodes)
end
end

Expand Down

0 comments on commit 3a2ff83

Please sign in to comment.