Skip to content

Commit

Permalink
optimize SVector4 and improve type stability
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Nov 11, 2024
1 parent 3a2ff83 commit 5dd180b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/common_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,31 @@ Base.getindex(im::IntMap, ind...) = getindex(im.map, ind...)
Base.setindex!(im::IntMap, v, ind...) = setindex!(im.map, v, ind...)

##### SVector4
mutable struct SVector4{T}
mutable struct SVector4Core{T}
e1::T
e2::T
e3::T
e4::T
SVector4Core{T}() where T = new()
end
mutable struct SVector4{T}
vec::SVector4Core{T}
len::Int
SVector4{T}() where T = (v = new(); v.len = 0; v)
end
SVector4{T}() where T = SVector4{T}(SVector4Core{T}(), 0)
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.vec.e1 = e1
v.vec.e2 = e2
v.vec.e3 = e3
v.vec.e4 = e4
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
Base.getindex(v::SVector4{T}, i) where T = getfield(v.vec, i)::T
Base.setindex!(v::SVector4{T}, x::T, i) where T = setfield!(v.vec, i, x)
Base.push!(v::SVector4{T}, x::T) where T = 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
Expand Down
4 changes: 2 additions & 2 deletions src/qtree_functions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
########## totalcollisions
function collision_dfs(Q1::AbstractStackedQTree, Q2::AbstractStackedQTree, i=(length(Q1), 1, 1)) #faster than _collision_randbfs (6:7)
function collision_dfs(Q1::AbstractStackedQTree, Q2::AbstractStackedQTree, i::Index=(length(Q1), 1, 1)) #faster than _collision_randbfs (6:7)
# @assert size(Q1) == size(Q2)
code = Q1[i] & Q2[i]
if code == FULL # Q1[i] == FULL || Q2[i] == FULL && (Q1[i] != EMPTY && Q2[i] != EMPTY)
Expand Down Expand Up @@ -138,7 +138,7 @@ function linked_spacial_qtree(qts)
end
hash_spacial_qtree() = HashSpacialQTree()
hash_spacial_qtree(qts) = hash_spacial_qtree()
function positionlower(qt, ind)
function positionlower(qt, ind::Index)
while ind[1] > 2
cind = ind
flag = false
Expand Down
4 changes: 2 additions & 2 deletions src/qtrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ function PaddedMat(l::T, sz::Tuple{Int,Int}=size(l), rshift=0, cshift=0; default
m.kernel[2:end - 2, 2:end - 2] .= l
m
end
function PaddedMat{T}(kernelsz::Tuple{Int,Int}, sz::Tuple{Int,Int}=size(l),
rshift=0, cshift=0; default=0x00) where {T <: AbstractMatrix{UInt8}}
function PaddedMat{T}(kernelsz::Tuple{Int,Int}, sz::Tuple{Int,Int},
rshift=0, cshift=0; default=zero(T)) where {T <: AbstractMatrix{UInt8}}
k = similar(T, kernelsz .+ 3) # +3 to keep top-down getindex in _collision_randbfs within the kernel bounds
k[[1, end - 1, end], :] .= default
k[:, [1, end - 1, end]] .= default
Expand Down

0 comments on commit 5dd180b

Please sign in to comment.