From 13d5973d0c3f35ced5bf7c0898cb4340095907f1 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Mon, 19 Jul 2021 13:27:06 -0700 Subject: [PATCH 1/8] Allow AbstractVectors --- src/sorttwo!.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorttwo!.jl b/src/sorttwo!.jl index ef78d00..99f855d 100644 --- a/src/sorttwo!.jl +++ b/src/sorttwo!.jl @@ -6,7 +6,7 @@ import StatsBase: BaseRadixSortSafeTypes Sort both the `vs` and reorder `index` at the same. This allows for faster sortperm for radix sort. """ -function sorttwo!(vs::Vector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes} +function sorttwo!(vs::AbstractVector{T}, index=1:length(vs), lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes} # Input checking if lo >= hi; return (vs, index); end #println(vs) From 33dafa8ed45148af7dfd1b833147a2bc9956b647 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Mon, 19 Jul 2021 14:11:58 -0700 Subject: [PATCH 2/8] Vector -> AbstractVector --- src/fsort.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fsort.jl b/src/fsort.jl index 19db910..6aa9cd7 100644 --- a/src/fsort.jl +++ b/src/fsort.jl @@ -66,5 +66,5 @@ export fsort, fsort! -fsort(x::Vector{T}; rev = false) where T <:BaseRadixSortSafeTypes = sort(x, alg = RadixSort; rev = rev) -fsort!(x::Vector{T}; rev = false) where T <:BaseRadixSortSafeTypes = sort!(x, alg = RadixSort; rev = rev) +fsort(x::AbstractVector{T}; rev = false) where T <:BaseRadixSortSafeTypes = sort(x, alg = RadixSort; rev = rev) +fsort!(x::AbstractVector{T}; rev = false) where T <:BaseRadixSortSafeTypes = sort!(x, alg = RadixSort; rev = rev) From 7127be6e88cbf896a5e18b28daad739c05f97b31 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Mon, 19 Jul 2021 14:12:39 -0700 Subject: [PATCH 3/8] Update uint_hist.jl --- src/uint_hist.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uint_hist.jl b/src/uint_hist.jl index 8ad2b18..55f8ed6 100644 --- a/src/uint_hist.jl +++ b/src/uint_hist.jl @@ -5,7 +5,7 @@ Computes a histogram (counts) for the vector RADIX_SIZE bits at a time. E.g. if eltype(bits) is UInt64 and RADIX_SIZE is 16 then 4 histograms are created for each of the 16 bit chunks. """ -function uint_hist(bits::Vector{T}, RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T +function uint_hist(bits::AbstractVector{T}, RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T iter = ceil(Integer, sizeof(T)*8/RADIX_SIZE) hist = zeros(UInt32, 2^RADIX_SIZE, iter) From 0824c593a8de45bdef8a0f5ff74981faff215b20 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Mon, 19 Jul 2021 14:14:28 -0700 Subject: [PATCH 4/8] AbstractVector method --- src/radixsort-StrFs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/radixsort-StrFs.jl b/src/radixsort-StrFs.jl index 9295059..64d0059 100644 --- a/src/radixsort-StrFs.jl +++ b/src/radixsort-StrFs.jl @@ -15,7 +15,7 @@ SortingAlgorithms.uint_mapping(::Base.Order.ForwardOrdering, s::StrF{S}) where S ((res << (8*(16-sizeof(s)))) >> (8*(16-sizeof(s)))) |> ntoh end -radixsort(v::Vector{StrF{S}}; rev = false) where S = begin +radixsort(v::AbstractVector{StrF{S}}; rev = false) where S = begin if S <=16 return sort(v, alg = RadixSort, rev = rev) else @@ -23,7 +23,7 @@ radixsort(v::Vector{StrF{S}}; rev = false) where S = begin end end -radixsort!(v::Vector{StrF{S}}; rev = false) where S = begin +radixsort!(v::AbstractVector{StrF{S}}; rev = false) where S = begin if S <=16 return sort!(v, alg = RadixSort, rev = rev) else From 8bfcacd0d860f4cea52604bc310ddd7b1bed3ad1 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Mon, 19 Jul 2021 14:16:58 -0700 Subject: [PATCH 5/8] Vector -> AbstractVector --- src/fsortperm_Integer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fsortperm_Integer.jl b/src/fsortperm_Integer.jl index fa4af2d..981802e 100644 --- a/src/fsortperm_Integer.jl +++ b/src/fsortperm_Integer.jl @@ -190,7 +190,7 @@ end # based on sortperm_int_range_p1 # see https://discourse.julialang.org/t/ironic-observation-about-sort-and-sortperm-speed-for-small-intergers-vs-r/8715/31?u=xiaodai ################################################################################ -function fsortperm_int_range_lsd(a::Vector{T}, rangelen, minval, RADIX_SIZE; rev = false) where T +function fsortperm_int_range_lsd(a::AbstractVector{T}, rangelen, minval, RADIX_SIZE; rev = false) where T # println(RADIX_SIZE," ", now()) # @assert 2^32 > rangelen # @assert 2^32 >= length(a) @@ -211,7 +211,7 @@ function fsortperm_int_range_lsd(a::Vector{T}, rangelen, minval, RADIX_SIZE; rev [Int(vs1.first) for vs1 in vs] end -function fsortandperm_int_range_lsd(a::Vector{T}, rangelen, minval, RADIX_SIZE; rev = false) where T +function fsortandperm_int_range_lsd(a::AbstractVector{T}, rangelen, minval, RADIX_SIZE; rev = false) where T vs = Vector{Pair{UInt32, T}}(length(a)) if rev maxval = maximum(a) From 592171e5d128010fa8a9fae9143b8d24bc9fe1ac Mon Sep 17 00:00:00 2001 From: Closed-Limelike-Curves Date: Mon, 19 Jul 2021 18:56:58 -0700 Subject: [PATCH 6/8] remove default arg added by accident --- .lh/src/sorttwo!.jl.json | 18 ++++++++++++++++++ src/sorttwo!.jl | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .lh/src/sorttwo!.jl.json diff --git a/.lh/src/sorttwo!.jl.json b/.lh/src/sorttwo!.jl.json new file mode 100644 index 0000000..84758bb --- /dev/null +++ b/.lh/src/sorttwo!.jl.json @@ -0,0 +1,18 @@ +{ + "sourceFile": "src/sorttwo!.jl", + "activeCommit": 0, + "commits": [ + { + "activePatchIndex": 0, + "patches": [ + { + "date": 1626746187070, + "content": "Index: \n===================================================================\n--- \n+++ \n" + } + ], + "date": 1626746187070, + "name": "Commit-0", + "content": "import StatsBase: BaseRadixSortSafeTypes\n\n\"\"\"\n sorttwo!(vs, index)\n\nSort both the `vs` and reorder `index` at the same. This allows for faster sortperm\nfor radix sort.\n\"\"\"\nfunction sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes}\n # Input checking\n if lo >= hi; return (vs, index); end\n #println(vs)\n o = Forward\n\n # Init\n iters = ceil(Integer, sizeof(T)*8/RADIX_SIZE)\n # number of buckets in the counting steuint_histp\n nbuckets = 2^RADIX_SIZE\n\n # Histogram for each element, radix\n bin = uint_hist(vs, RADIX_SIZE, RADIX_MASK)\n\n # bin = zeros(UInt32, nbuckets, iters)\n # if lo > 1; bin[1,:] = lo-1; end\n\n # Sort!\n swaps = 0\n len = hi-lo+1\n\n index1 = similar(index)\n ts=similar(vs)\n for j = 1:iters\n # Unroll first data iteration, check for degenerate case\n v = uint_mapping(o, vs[hi])\n idx = Int((v >> ((j-1)*RADIX_SIZE)) & RADIX_MASK) + 1\n\n # are all values the same at this radix?\n if bin[idx,j] == len; continue; end\n\n # cbin = cumsum(bin[:,j])\n # tries to achieve the above one-liner with more efficiency\n cbin = zeros(UInt32, nbuckets)\n cbin[1] = bin[1,j]\n @inbounds for i in 2:nbuckets\n cbin[i] = cbin[i-1] + bin[i,j]\n end\n\n ci = cbin[idx]\n #println((ci, hi))\n ts[ci] = vs[hi]\n index1[ci] = index[hi]\n # println(cbin[idx])\n cbin[idx] -= 1\n\n # Finish the loop... \n @inbounds for i in hi-1:-1:lo\n v = uint_mapping(o, vs[i])\n idx = Int((v >> ((j-1)*RADIX_SIZE)) & RADIX_MASK) + 1\n ci = cbin[idx]\n #println(ci)\n ts[ci] = vs[i]\n index1[ci] = index[i]\n cbin[idx] -= 1\n end\n vs,ts = ts,vs\n index, index1 = index1, index\n swaps += 1\n end\n\n if isodd(swaps)\n vs,ts = ts,vs\n index, index1 = index1, index\n for i = lo:hi\n @inbounds vs[i] = ts[i]\n @inbounds index[i] = index1[i]\n end\n end\n (vs, index)\nend\n" + } + ] +} \ No newline at end of file diff --git a/src/sorttwo!.jl b/src/sorttwo!.jl index 99f855d..1995cc8 100644 --- a/src/sorttwo!.jl +++ b/src/sorttwo!.jl @@ -6,7 +6,7 @@ import StatsBase: BaseRadixSortSafeTypes Sort both the `vs` and reorder `index` at the same. This allows for faster sortperm for radix sort. """ -function sorttwo!(vs::AbstractVector{T}, index=1:length(vs), lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes} +function sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes} # Input checking if lo >= hi; return (vs, index); end #println(vs) From 0966238fe69c60636a78f1966eb30d4711863238 Mon Sep 17 00:00:00 2001 From: Closed-Limelike-Curves Date: Mon, 19 Jul 2021 19:10:34 -0700 Subject: [PATCH 7/8] Check type (safer if users call `sorttwo!`) --- src/sorttwo!.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorttwo!.jl b/src/sorttwo!.jl index 1995cc8..b6add29 100644 --- a/src/sorttwo!.jl +++ b/src/sorttwo!.jl @@ -6,7 +6,7 @@ import StatsBase: BaseRadixSortSafeTypes Sort both the `vs` and reorder `index` at the same. This allows for faster sortperm for radix sort. """ -function sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes} +function sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where {T<:Union{BaseRadixSortSafeTypes}} # Input checking if lo >= hi; return (vs, index); end #println(vs) From 56dce31cb96ef2591307faf3b180db8a583e2b0e Mon Sep 17 00:00:00 2001 From: Closed-Limelike-Curves Date: Mon, 19 Jul 2021 19:11:41 -0700 Subject: [PATCH 8/8] Enable Missing --- src/sorttwo!.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorttwo!.jl b/src/sorttwo!.jl index b6add29..625e813 100644 --- a/src/sorttwo!.jl +++ b/src/sorttwo!.jl @@ -6,7 +6,7 @@ import StatsBase: BaseRadixSortSafeTypes Sort both the `vs` and reorder `index` at the same. This allows for faster sortperm for radix sort. """ -function sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where {T<:Union{BaseRadixSortSafeTypes}} +function sorttwo!(vs::AbstractVector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T<:Union{BaseRadixSortSafeTypes, Missing} # Input checking if lo >= hi; return (vs, index); end #println(vs)