From e164e5549a44a75cb11c1a823fd7b2f840b199a1 Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Mon, 6 Feb 2023 17:14:48 -0800 Subject: [PATCH 01/10] Update algo for tfm:Wenbo() --- src/wenbo.jl | 94 +++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index 9c55a70..e04e04b 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -112,74 +112,68 @@ md""" """ # ╔═╡ 16991d8b-ec84-49d0-90a9-15a78f1668bb -function _encode(leftD, rightf) - if rightf == 1f10 - return -leftD - end - idx = 0 - while rightf>1 - rightf /=10 - idx+=1 - end - return -leftD-idx/10-rightf/10 -end +# function _encode(leftD, rightf) +# if rightf == 1f10 +# return -leftD +# end +# idx = 0 +# while rightf>1 +# rightf /=10 +# idx+=1 +# end +# return -leftD-idx/10-rightf/10 +# end # ╔═╡ e7dbc916-c5cb-4f86-8ea1-adbcb0bdf8ea -function _decode(curr) - curr *= -10 - temp = Int(floor(curr)) - curr -= temp - if curr == 0 - return 1f10 - end - temp %= 10 - while temp > 0 - temp -= 1 - curr*=10 - end - return round(curr) -end +# function _decode(curr) +# curr *= -10 +# temp = Int(floor(curr)) +# curr -= temp +# if curr == 0 +# return 1f10 +# end +# temp %= 10 +# while temp > 0 +# temp -= 1 +# curr*=10 +# end +# return round(curr) +# end # ╔═╡ 32a4bf03-98f8-4ed9-9c12-f45c09b0b0dd -function _transform2!(f::AbstractVector) +function _transform2!(f::AbstractVector, org::AbstractVector) l = length(f) pointerA = 1 - while pointerA<=l && @inbounds f[pointerA] <= 1 + while pointerA<=l && @inbounds f[pointerA] <= 1f0 pointerA += 1 end p = 0 while pointerA<=l @inbounds curr = f[pointerA] - prev = curr + # left temp = min(pointerA-1, p+1) p = 0 while 0 < temp - @inbounds fi = f[pointerA-temp] - fi = fi < 0 ? _decode(fi) : fi - newDistance = muladd(temp, temp, fi) + @inbounds newDistance = muladd(temp, temp, org[pointerA-temp]) if newDistance < curr curr = newDistance p = temp end temp -= 1 end + # right temp = 1 templ = length(f) - pointerA while temp <= templ && muladd(temp, temp, -curr) < 0 @inbounds curr = min(curr, muladd(temp, temp, f[pointerA+temp])) temp += 1 end - @inbounds f[pointerA] = _encode(curr, prev) + @inbounds f[pointerA] = curr pointerA+=1 - while pointerA<=l && @inbounds f[pointerA] <= 1 + while pointerA<=l && @inbounds f[pointerA] <= 1f0 pointerA += 1 end end - i = 0 - while i Date: Thu, 9 Feb 2023 14:20:10 -0800 Subject: [PATCH 02/10] Fixed in 3D dt --- src/wenbo.jl | 68 ++++++++++++++++++++++++++++++--------------------- test/wenbo.jl | 2 +- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index e04e04b..15d7503 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -177,15 +177,15 @@ function _transform2!(f::AbstractVector, org::AbstractVector) end # ╔═╡ 89fed2a6-b09e-47b1-a020-efed76ba57de -function _transform3!(f) - for i in axes(f, 1) - @inbounds _transform1!(@view(f[i, :])) - end - org = deepcopy(f) - for j in axes(f, 2) - @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) - end -end +# function _transform3!(f) +# for i in axes(f, 1) +# @inbounds _transform1!(@view(f[i, :])) +# end +# org = deepcopy(f) +# for j in axes(f, 2) +# @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) +# end +# end # ╔═╡ 423df2ac-b9a2-4d59-b5fc-8de0e8cc6691 """ @@ -222,12 +222,16 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo) f = boolean_indicator(f) - for i in axes(f, 3) - @inbounds _transform3!(@view(f[:, :, i])) + for i in CartesianIndices(f[:,1,:]) + @inbounds _transform1!(@view(f[i[1], :, i[2]])) end org = deepcopy(f) - for j in CartesianIndices(f[:,:,1]) - @inbounds _transform2!(@view(f[j, :]), @view(org[j, :])) + for i in CartesianIndices(f[1,:,:]) + @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) + end + org = deepcopy(f) + for i in CartesianIndices(f[:,:,1]) + @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end return f end @@ -238,15 +242,15 @@ md""" """ # ╔═╡ d663cf13-4a3a-4667-8971-ddb5c455d85c -function _transform4!(f) - Threads.@threads for i in axes(f, 1) - @inbounds _transform1!(@view(f[i, :])) - end - org = deepcopy(f) - Threads.@threads for j in axes(f, 2) - @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) - end -end +# function _transform4!(f) +# Threads.@threads for i in axes(f, 1) +# @inbounds _transform1!(@view(f[i, :])) +# end +# org = deepcopy(f) +# Threads.@threads for j in axes(f, 2) +# @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) +# end +# end # ╔═╡ 0f0675ad-899d-4808-9757-deaae19a58a5 md""" @@ -288,12 +292,16 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo, nthreads::Number) f = boolean_indicator(f) - Threads.@threads for i in axes(f, 3) - @inbounds _transform4!(@view(f[:, :, i])) + Threads.@threads for i in CartesianIndices(f[:,1,:]) + @inbounds _transform1!(@view(f[i[1], :, i[2]])) end org = deepcopy(f) - Threads.@threads for j in CartesianIndices(f[:,:,1]) - @inbounds _transform2!(@view(f[j, :]), @view(org[j, :])) + Threads.@threads for i in CartesianIndices(f[1,:,:]) + @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) + end + org = deepcopy(f) + Threads.@threads for i in CartesianIndices(f[:,:,1]) + @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end return f end @@ -1031,8 +1039,12 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo, ex) f = boolean_indicator(f) - @floop ex for k in axes(f, 3) - @inbounds _transform4!(@view(f[:, :, k])) + @floop ex for i in CartesianIndices(f[:,1,:]) + @inbounds _transform1!(@view(f[i[1], :, i[2]])) + end + org = deepcopy(f) + @floop ex for i in CartesianIndices(f[1,:,:]) + @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) end org = deepcopy(f) @floop ex for i in CartesianIndices(f[:,:,1]) diff --git a/test/wenbo.jl b/test/wenbo.jl index 3e2c2a0..5aa224d 100644 --- a/test/wenbo.jl +++ b/test/wenbo.jl @@ -1,5 +1,5 @@ ### A Pluto.jl notebook ### -# v0.19.14 +# v0.19.22 using Markdown using InteractiveUtils From 7e85b8007569543b1a220e685b24f33781157352 Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 14:31:38 -0800 Subject: [PATCH 03/10] bug fixed --- src/wenbo.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index 15d7503..bf5209d 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -222,12 +222,12 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo) f = boolean_indicator(f) - for i in CartesianIndices(f[:,1,:]) - @inbounds _transform1!(@view(f[i[1], :, i[2]])) + for i in CartesianIndices(f[1,:,:]) + @inbounds _transform1!(@view(f[:, i])) end org = deepcopy(f) - for i in CartesianIndices(f[1,:,:]) - @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) + for i in CartesianIndices(f[:,1,:]) + @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end org = deepcopy(f) for i in CartesianIndices(f[:,:,1]) @@ -292,12 +292,12 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo, nthreads::Number) f = boolean_indicator(f) - Threads.@threads for i in CartesianIndices(f[:,1,:]) - @inbounds _transform1!(@view(f[i[1], :, i[2]])) + Threads.@threads for i in CartesianIndices(f[1,:,:]) + @inbounds _transform1!(@view(f[:, i])) end org = deepcopy(f) - Threads.@threads for i in CartesianIndices(f[1,:,:]) - @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) + Threads.@threads for i in CartesianIndices(f[:,1,:]) + @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end org = deepcopy(f) Threads.@threads for i in CartesianIndices(f[:,:,1]) @@ -1039,12 +1039,12 @@ Applies a squared euclidean distance transform to an input 3D image using the We """ function transform(f::AbstractArray, tfm::Wenbo, ex) f = boolean_indicator(f) - @floop ex for i in CartesianIndices(f[:,1,:]) - @inbounds _transform1!(@view(f[i[1], :, i[2]])) + @floop ex for i in CartesianIndices(f[1,:,:]) + @inbounds _transform1!(@view(f[:, i])) end org = deepcopy(f) - @floop ex for i in CartesianIndices(f[1,:,:]) - @inbounds _transform2!(@view(f[:, i]), @view(org[:, i])) + @floop ex for i in CartesianIndices(f[:,1,:]) + @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end org = deepcopy(f) @floop ex for i in CartesianIndices(f[:,:,1]) From 8fa82973541fd8b870a99061b4d4c09e9c8b3eb4 Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 14:46:45 -0800 Subject: [PATCH 04/10] Added debug code --- src/wenbo.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wenbo.jl b/src/wenbo.jl index bf5209d..2d6d827 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -233,6 +233,7 @@ function transform(f::AbstractArray, tfm::Wenbo) for i in CartesianIndices(f[:,:,1]) @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end + println("on branch: wenbo") return f end From 44407b63b8cf8abfd1994a9c3eff7325920f323a Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 14:57:20 -0800 Subject: [PATCH 05/10] bug fixed --- src/wenbo.jl | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index 2d6d827..057735d 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -221,20 +221,20 @@ transform(f::AbstractArray, tfm::Wenbo) Applies a squared euclidean distance transform to an input 3D image using the Wenbo algorithm. Returns an array with spatial information embedded in the array elements. """ function transform(f::AbstractArray, tfm::Wenbo) - f = boolean_indicator(f) - for i in CartesianIndices(f[1,:,:]) - @inbounds _transform1!(@view(f[:, i])) - end - org = deepcopy(f) - for i in CartesianIndices(f[:,1,:]) - @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) - end - org = deepcopy(f) - for i in CartesianIndices(f[:,:,1]) - @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) - end - println("on branch: wenbo") - return f + # f = boolean_indicator(f) + # for i in CartesianIndices(f[1,:,:]) + # @inbounds _transform1!(@view(f[:, i])) + # end + # org = deepcopy(f) + # for i in CartesianIndices(f[:,1,:]) + # @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) + # end + # org = deepcopy(f) + # for i in CartesianIndices(f[:,:,1]) + # @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) + # end + # return f + return 0 end # ╔═╡ 58e1cdff-59b8-44d9-a1b7-ecc14b09556c From db13eb840936398cd97d25acea31568a2b1bcdcf Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 15:00:29 -0800 Subject: [PATCH 06/10] Removed debug code --- src/wenbo.jl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index 057735d..bf5209d 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -221,20 +221,19 @@ transform(f::AbstractArray, tfm::Wenbo) Applies a squared euclidean distance transform to an input 3D image using the Wenbo algorithm. Returns an array with spatial information embedded in the array elements. """ function transform(f::AbstractArray, tfm::Wenbo) - # f = boolean_indicator(f) - # for i in CartesianIndices(f[1,:,:]) - # @inbounds _transform1!(@view(f[:, i])) - # end - # org = deepcopy(f) - # for i in CartesianIndices(f[:,1,:]) - # @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) - # end - # org = deepcopy(f) - # for i in CartesianIndices(f[:,:,1]) - # @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) - # end - # return f - return 0 + f = boolean_indicator(f) + for i in CartesianIndices(f[1,:,:]) + @inbounds _transform1!(@view(f[:, i])) + end + org = deepcopy(f) + for i in CartesianIndices(f[:,1,:]) + @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) + end + org = deepcopy(f) + for i in CartesianIndices(f[:,:,1]) + @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) + end + return f end # ╔═╡ 58e1cdff-59b8-44d9-a1b7-ecc14b09556c From 008a01dacabd111d3728a89f379a44b83cd71bc7 Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 15:41:44 -0800 Subject: [PATCH 07/10] replaced with --- src/wenbo.jl | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index bf5209d..58264c0 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -181,7 +181,7 @@ end # for i in axes(f, 1) # @inbounds _transform1!(@view(f[i, :])) # end -# org = deepcopy(f) +# org = copy(f) # for j in axes(f, 2) # @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) # end @@ -200,7 +200,7 @@ function transform(f::AbstractMatrix, tfm::Wenbo) for i in axes(f, 1) @inbounds _transform1!(@view(f[i, :])) end - org = deepcopy(f) + org = copy(f) for j in axes(f, 2) @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) end @@ -225,11 +225,11 @@ function transform(f::AbstractArray, tfm::Wenbo) for i in CartesianIndices(f[1,:,:]) @inbounds _transform1!(@view(f[:, i])) end - org = deepcopy(f) + org = copy(f) for i in CartesianIndices(f[:,1,:]) @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end - org = deepcopy(f) + org = copy(f) for i in CartesianIndices(f[:,:,1]) @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end @@ -246,7 +246,7 @@ md""" # Threads.@threads for i in axes(f, 1) # @inbounds _transform1!(@view(f[i, :])) # end -# org = deepcopy(f) +# org = copy(f) # Threads.@threads for j in axes(f, 2) # @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) # end @@ -270,7 +270,7 @@ function transform(f::AbstractMatrix, tfm::Wenbo, nthreads::Number) Threads.@threads for i in axes(f, 1) @inbounds _transform1!(@view(f[i, :])) end - org = deepcopy(f) + org = copy(f) Threads.@threads for j in axes(f, 2) @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) end @@ -295,11 +295,11 @@ function transform(f::AbstractArray, tfm::Wenbo, nthreads::Number) Threads.@threads for i in CartesianIndices(f[1,:,:]) @inbounds _transform1!(@view(f[:, i])) end - org = deepcopy(f) + org = copy(f) Threads.@threads for i in CartesianIndices(f[:,1,:]) @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end - org = deepcopy(f) + org = copy(f) Threads.@threads for i in CartesianIndices(f[:,:,1]) @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end @@ -565,7 +565,7 @@ function _transform_batch(f::CuArray{T, 4}, tfm::Wenbo, kernels) where T for batch_idx = 1:batch_size for channel_idx = 1:num_channels @inbounds kernels[7](f_new, f, row_length, l, channel_idx, batch_idx; threads, blocks) - @inbounds kernels[8](deepcopy(f_new), f_new, row_length, col_length, l, channel_idx, batch_idx; threads, blocks) + @inbounds kernels[8](copy(f_new), f_new, row_length, col_length, l, channel_idx, batch_idx; threads, blocks) end end return f_new @@ -588,7 +588,7 @@ function transform(f::CuArray{T, 2}, tfm::Wenbo, kernels) where T blocks = cld(l, threads) # k1 = T<:Bool ? kernels[2] : kernels[1] @inbounds kernels[1](f_new, f, row_length, l; threads, blocks) - @inbounds kernels[2](deepcopy(f_new), f_new, row_length, col_length, l; threads, blocks) + @inbounds kernels[2](copy(f_new), f_new, row_length, col_length, l; threads, blocks) return f_new end @@ -936,8 +936,8 @@ function _transform_batch(f::CuArray{T, 5}, tfm::Wenbo, kernels) where T for batch_idx = 1:batch_size for channel_idx = 1:num_channels @inbounds kernels[9](f_new, f, d2, d3, l, channel_idx, batch_idx; threads, blocks) - @inbounds kernels[10](f_new, deepcopy(f_new), d1, d2, d3, l, channel_idx, batch_idx; threads, blocks) - @inbounds kernels[11](f_new, deepcopy(f_new), d2, d3, l, channel_idx, batch_idx; threads, blocks) + @inbounds kernels[10](f_new, copy(f_new), d1, d2, d3, l, channel_idx, batch_idx; threads, blocks) + @inbounds kernels[11](f_new, copy(f_new), d2, d3, l, channel_idx, batch_idx; threads, blocks) end end return f_new @@ -959,8 +959,8 @@ function transform(f::CuArray{T, 3}, tfm::Wenbo, kernels) where T blocks = cld(l, threads) # k1 = T<:Bool ? kernels[5] : kernels[4] @inbounds kernels[3](f_new, f, d2, d3, l; threads, blocks) - @inbounds kernels[4](f_new, deepcopy(f_new), d1, d2, d3, l; threads, blocks) - @inbounds kernels[5](f_new, deepcopy(f_new), d2, d3, l; threads, blocks) + @inbounds kernels[4](f_new, copy(f_new), d1, d2, d3, l; threads, blocks) + @inbounds kernels[5](f_new, copy(f_new), d2, d3, l; threads, blocks) return f_new end @@ -1017,7 +1017,7 @@ function transform(f::AbstractMatrix, tfm::Wenbo, ex) @floop ex for i in axes(f, 1) @inbounds _transform1!(@view(f[i, :])) end - org = deepcopy(f) + org = copy(f) @floop ex for j in axes(f, 2) @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) end @@ -1042,11 +1042,11 @@ function transform(f::AbstractArray, tfm::Wenbo, ex) @floop ex for i in CartesianIndices(f[1,:,:]) @inbounds _transform1!(@view(f[:, i])) end - org = deepcopy(f) + org = copy(f) @floop ex for i in CartesianIndices(f[:,1,:]) @inbounds _transform2!(@view(f[i[1], :, i[2]]), @view(org[i[1], :, i[2]])) end - org = deepcopy(f) + org = copy(f) @floop ex for i in CartesianIndices(f[:,:,1]) @inbounds _transform2!(@view(f[i, :]), @view(org[i, :])) end From 1139e2a74cc71d684bef8819ae8cd59ae6ffb6fd Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 15:58:21 -0800 Subject: [PATCH 08/10] Updated tests --- test/wenbo.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/wenbo.jl b/test/wenbo.jl index 3e2c2a0..c478dae 100644 --- a/test/wenbo.jl +++ b/test/wenbo.jl @@ -286,7 +286,7 @@ if CUDA.has_cuda_gpu() 0 1 1 1 0 0 0 0 1 ] tfm = Wenbo() - test = transform(CuArray(img), tfm, ks) + test = transform(CuArray(Float32.(img)), tfm, ks) answer = CuArray([ 1.0 0.0 0.0 0.0 1.0 2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 2.0 1.0 0.0 @@ -308,7 +308,7 @@ if CUDA.has_cuda_gpu() img = rand([0, 1], 10, 10) img2 = copy(img) tfm = Wenbo() - test = transform(CUDA.CuArray(img), tfm, ks) + test = transform(CUDA.CuArray(Float32.(img)), tfm, ks) answer = transform(img2, tfm) @test test == CuArray(answer) end @@ -344,7 +344,7 @@ if CUDA.has_cuda_gpu() for i in 1:10 push!(container2, vol) end - vol_inv = CuArray(cat(container2..., dims=3)) + vol_inv = CuArray(Float32.(cat(container2..., dims=3))) tfm = Wenbo() test = transform(vol_inv, tfm, ks) a1 = img_inv @@ -367,7 +367,7 @@ if CUDA.has_cuda_gpu() img = rand([0, 1], 10, 10, 10) img2 = copy(img) tfm = Wenbo() - test = transform(CUDA.CuArray(img), tfm, ks) + test = transform(CUDA.CuArray(Float32.(img)), tfm, ks) answer = transform(img2, tfm) @test test == CuArray(answer) end From 4b6fe77d898dfc83ae8cb7d484ec60aacf05b12c Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Thu, 9 Feb 2023 15:59:25 -0800 Subject: [PATCH 09/10] Updated tests --- test/wenbo.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/wenbo.jl b/test/wenbo.jl index 29bfe89..c478dae 100644 --- a/test/wenbo.jl +++ b/test/wenbo.jl @@ -1,5 +1,5 @@ ### A Pluto.jl notebook ### -# v0.19.22 +# v0.19.14 using Markdown using InteractiveUtils From 5f63c65d3848e1fef197e9ef5110eafa1d92c266 Mon Sep 17 00:00:00 2001 From: Wenbo Li Date: Wed, 15 Feb 2023 16:25:47 -0800 Subject: [PATCH 10/10] fixed bug in EN+DE --- src/wenbo.jl | 132 ++++++++++++++++++++++++++++++-------------------- test/wenbo.jl | 2 +- 2 files changed, 80 insertions(+), 54 deletions(-) diff --git a/src/wenbo.jl b/src/wenbo.jl index 58264c0..9ab7124 100644 --- a/src/wenbo.jl +++ b/src/wenbo.jl @@ -50,7 +50,7 @@ function _transform1!(f::AbstractVector) pointerA = 1 l = length(f) while pointerA <= l - while pointerA <= l && @inbounds f[pointerA] == 0 + while pointerA <= l && @inbounds f[pointerA] == 0f0 pointerA+=1 end pointerB = pointerA @@ -112,33 +112,34 @@ md""" """ # ╔═╡ 16991d8b-ec84-49d0-90a9-15a78f1668bb -# function _encode(leftD, rightf) -# if rightf == 1f10 -# return -leftD -# end -# idx = 0 -# while rightf>1 -# rightf /=10 -# idx+=1 -# end -# return -leftD-idx/10-rightf/10 -# end +function _encode(leftD::Float32, rightf::Float32) + if rightf == 1f10 + return -leftD + end + idx = 0f0 + while rightf >= 1f0 + rightf /= 10f0 + idx += 1f0 + end + return -leftD-idx/10f0-rightf/10f0 +end # ╔═╡ e7dbc916-c5cb-4f86-8ea1-adbcb0bdf8ea -# function _decode(curr) -# curr *= -10 -# temp = Int(floor(curr)) -# curr -= temp -# if curr == 0 -# return 1f10 -# end -# temp %= 10 -# while temp > 0 -# temp -= 1 -# curr*=10 -# end -# return round(curr) -# end +function _decode(curr::Float32) + curr *= -1f0 + curr -= floor(curr) + curr *= 10f0 + temp = floor(curr % 10f0) + if temp == 0f0 + return 1f10 + end + curr -= temp + while temp > 0f0 + temp -= 1f0 + curr *= 10f0 + end + return round(curr) +end # ╔═╡ 32a4bf03-98f8-4ed9-9c12-f45c09b0b0dd function _transform2!(f::AbstractVector, org::AbstractVector) @@ -177,15 +178,48 @@ function _transform2!(f::AbstractVector, org::AbstractVector) end # ╔═╡ 89fed2a6-b09e-47b1-a020-efed76ba57de -# function _transform3!(f) -# for i in axes(f, 1) -# @inbounds _transform1!(@view(f[i, :])) -# end -# org = copy(f) -# for j in axes(f, 2) -# @inbounds _transform2!(@view(f[:,j]), @view(org[:,j])) -# end -# end +function _transform2_EN_DE!(f::AbstractVector) + l = length(f) + pointerA = 1 + while pointerA<=l && @inbounds f[pointerA] <= 1f0 + pointerA += 1 + end + p = 0 + while pointerA<=l + @inbounds curr = f[pointerA] + # left + prev = curr + temp = min(pointerA-1, p+1) + p = 0 + while 0 < temp + @inbounds fi = f[pointerA-temp] + fi = fi < 0 ? _decode(fi) : fi + newDistance = muladd(temp, temp, fi) + if newDistance < curr + curr = newDistance + p = temp + end + temp -= 1 + end + # right + temp = 1 + templ = length(f) - pointerA + while temp <= templ && muladd(temp, temp, -curr) < 0 + @inbounds curr = min(curr, muladd(temp, temp, f[pointerA+temp])) + temp += 1 + end + @inbounds f[pointerA] = _encode(curr, prev) + pointerA+=1 + while pointerA<=l && @inbounds f[pointerA] <= 1f0 + pointerA += 1 + end + end + i = 0 + while i