Skip to content

Allow AbstractVectors #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .lh/src/sorttwo!.jl.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
4 changes: 2 additions & 2 deletions src/fsort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/fsortperm_Integer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/radixsort-StrFs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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
throw(ErrorException("radix sort of Vector{StrF(S)} is only possible for S <= 16 for now"))
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
Expand Down
2 changes: 1 addition & 1 deletion src/sorttwo!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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)
Expand Down
2 changes: 1 addition & 1 deletion src/uint_hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down