Skip to content

Commit 2a4088c

Browse files
authored
Merge pull request #7 from xiaodaigh/missing
support sorting of missing
2 parents afe6681 + 5b47d58 commit 2a4088c

12 files changed

+45
-6
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.2.2
2+
* Supports `sort` `Union{T, Missing}`
3+
* Supports `fsortperm` of `Union{T, Missing}`
4+
15
## Version 0.2.1
26

37
* fsortperm now sorts many more types including Floats and Int

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
name = "SortingLab"
22
uuid = "562c1548-17b8-5b69-83cf-d8aebec229f5"
33
authors = ["Dai ZJ <zhuojia.dai@gmail.com>"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

66
[deps]
77
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
88
SortingAlgorithms = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
99
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1010

1111
[compat]
12-
julia = "1"
1312
CategoricalArrays = "0.7"
1413
SortingAlgorithms = "0.3"
1514
StatsBase = "0.32"
15+
julia = "1"
1616

1717
[extras]
1818
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ Test Passed
7878

7979

8080

81-
82-
8381
## Benchmark
8482
![Base.sort vs SortingLab.radixsort](benchmarks/sort_vs_radixsort.png)
8583

src/SortingLab.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module SortingLab
99
# import InternedStrings
1010
import StatsBase: countmap
1111
import CategoricalArrays: CategoricalArray
12-
import SortingAlgorithms: uint_mapping
1312
import Base.Threads: @threads
1413

1514
export fsortperm, radixsort, radixsort!, fsort, fsort!
@@ -24,6 +23,7 @@ include("fsortperm_string.jl")
2423
include("fsortperm_Integer.jl")
2524
include("fsort_CategoricalArrays.jl")
2625
include("fsort.jl")
26+
include("fsort-missing.jl")
2727
# include("radixsort-StrFs.jl")
2828
# include("../benchmarks/is_parallel_hist_faster_YES.jl")
2929
end # module

src/fsort-missing.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fsort(x::AbstractVector{Union{T, Missing}}; rev = false) where T = begin
2+
nmissing = mapreduce(ismissing, +, x)
3+
cx = similar(x)
4+
cx[1:length(x) - nmissing] .= fsort(collect(skipmissing(x)); rev = rev)
5+
cx[length(x)+1:end] .= missing
6+
cx
7+
end

src/fsortperm-missing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fsortperm(x::AbstractVector{Union{Missing, T}}) where T = begin
2+
n_missing = mapreduce(ismissing, +, x)
3+
res = Vector{Int}(undef, length(x))
4+
res[1:length(x)-n_missing] .= fsortperm(collect(skipmissing(x)))
5+
6+
end

src/sorttwo!.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import StatsBase: BaseRadixSortSafeTypes
66
Sort both the `vs` and reorder `index` at the same. This allows for faster sortperm
77
for radix sort.
88
"""
9-
function sorttwo!(vs::Vector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T <:Union{BaseRadixSortSafeTypes}
9+
function sorttwo!(vs::Vector{T}, index, lo::Int = 1, hi::Int=length(vs), RADIX_SIZE = 16, RADIX_MASK = 0xffff) where T # <:Union{BaseRadixSortSafeTypes}
1010
# Input checking
1111
if lo >= hi; return (vs, index); end
1212
#println(vs)

src/uint_hist.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# winner from benchmarks/which_is_the_fastest_UInt_histogram.jl
22
"""
33
uint_hist(bits, [RADIX_SIZE = 16, RADIX_MASK = 0xffff])
4+
45
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
56
then 4 histograms are created for each of the 16 bit chunks.
67
"""

src/uint_mapping.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
export uint_mapping
2+
13
import SortingAlgorithms: uint_mapping
24

35
uint_mapping(x) = uint_mapping(Base.Forward, x)
6+
7+
uint_mapping(::Missing) = UInt(2)^8sizeof(UInt)-1
8+
9+
uint_mapping(_, ::Missing) = UInt(2)^8sizeof(UInt)-1

test/fsort-missing.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Test
2+
using SortingLab
3+
4+
x = [1,2,missing, 3]
5+
6+
SortingLab.sorttwo!(x, collect(1:4))
7+
fsortperm(x)
8+
9+
@test isequal(sort(x), fsort(x))

0 commit comments

Comments
 (0)