@@ -11,7 +11,8 @@ using .Base: copymutable, LinearIndices, length, (:), iterate, OneTo,
1111 AbstractMatrix, AbstractUnitRange, isless, identity, eltype, > , < , <= , >= , | , + , - , * , ! ,
1212 extrema, sub_with_overflow, add_with_overflow, oneunit, div, getindex, setindex!,
1313 length, resize!, fill, Missing, require_one_based_indexing, keytype, UnitRange,
14- min, max, reinterpret, signed, unsigned, Signed, Unsigned, typemin, xor, Type, BitSigned, Val
14+ min, max, reinterpret, signed, unsigned, Signed, Unsigned, typemin, xor, Type, BitSigned, Val,
15+ midpoint
1516
1617using . Base: >>> , != =
1718
@@ -165,11 +166,6 @@ same thing as `partialsort!` but leaving `v` unmodified.
165166partialsort (v:: AbstractVector , k:: Union{Integer,OrdinalRange} ; kws... ) =
166167 partialsort! (copymutable (v), k; kws... )
167168
168- # This implementation of `midpoint` is performance-optimized but safe
169- # only if `lo <= hi`.
170- midpoint (lo:: T , hi:: T ) where T<: Integer = lo + ((hi - lo) >>> 0x01 )
171- midpoint (lo:: Integer , hi:: Integer ) = midpoint (promote (lo, hi)... )
172-
173169# reference on sorted binary search:
174170# http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary
175171
735731
736732# For AbstractVector{Bool}, counting sort is always best.
737733# This is an implementation of counting sort specialized for Bools.
738- # Accepts unused workspace to avoid method ambiguity.
734+ # Accepts unused buffer to avoid method ambiguity.
739735function sort! (v:: AbstractVector{B} , lo:: Integer , hi:: Integer , a:: AdaptiveSort , o:: Ordering ,
740736 t:: Union{AbstractVector{B}, Nothing} = nothing ) where {B <: Bool }
741737 first = lt (o, false , true ) ? false : lt (o, true , false ) ? true : return v
@@ -856,15 +852,15 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, a::AdaptiveSort,
856852 u[i] -= u_min
857853 end
858854
859- if t != = nothing && checkbounds (Bool, t, lo: hi) # Fully preallocated and aligned workspace
855+ if t != = nothing && checkbounds (Bool, t, lo: hi) # Fully preallocated and aligned buffer
860856 u2 = radix_sort! (u, lo, hi, bits, reinterpret (U, t))
861857 uint_unmap! (v, u2, lo, hi, o, u_min)
862- elseif t != = nothing && (applicable (resize!, t) || length (t) >= hi- lo+ 1 ) # Viable workspace
858+ elseif t != = nothing && (applicable (resize!, t) || length (t) >= hi- lo+ 1 ) # Viable buffer
863859 length (t) >= hi- lo+ 1 || resize! (t, hi- lo+ 1 )
864860 t1 = axes (t, 1 ) isa OneTo ? t : view (t, firstindex (t): lastindex (t))
865861 u2 = radix_sort! (view (u, lo: hi), 1 , hi- lo+ 1 , bits, reinterpret (U, t1))
866862 uint_unmap! (view (v, lo: hi), u2, 1 , hi- lo+ 1 , o, u_min)
867- else # No viable workspace
863+ else # No viable buffer
868864 u2 = radix_sort! (u, lo, hi, bits, similar (u))
869865 uint_unmap! (v, u2, lo, hi, o, u_min)
870866 end
@@ -933,8 +929,8 @@ function sort!(v::AbstractVector{T};
933929 by= identity,
934930 rev:: Union{Bool,Nothing} = nothing ,
935931 order:: Ordering = Forward,
936- workspace :: Union{AbstractVector{T}, Nothing} = nothing ) where T
937- sort! (v, alg, ord (lt,by,rev,order), workspace )
932+ buffer :: Union{AbstractVector{T}, Nothing} = nothing ) where T
933+ sort! (v, alg, ord (lt,by,rev,order), buffer )
938934end
939935
940936# sort! for vectors of few unique integers
@@ -1073,7 +1069,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
10731069 order:: Ordering = Forward,
10741070 initialized:: Bool = false )
10751071 if axes (ix,1 ) != axes (v,1 )
1076- throw (ArgumentError (" The index vector is used as a workspace and must have the " *
1072+ throw (ArgumentError (" The index vector is used as a buffer and must have the " *
10771073 " same length/indices as the source vector, $(axes (ix,1 )) != $(axes (v,1 )) " ))
10781074 end
10791075 if ! initialized
@@ -1140,7 +1136,7 @@ function sortperm(A::AbstractArray;
11401136 by= identity,
11411137 rev:: Union{Bool,Nothing} = nothing ,
11421138 order:: Ordering = Forward,
1143- workspace :: Union{AbstractVector{<:Integer}, Nothing} = nothing ,
1139+ buffer :: Union{AbstractVector{<:Integer}, Nothing} = nothing ,
11441140 dims... ) # to optionally specify dims argument
11451141 ordr = ord (lt,by,rev,order)
11461142 if ordr === Forward && isa (A,Vector) && eltype (A)<: Integer
@@ -1155,7 +1151,7 @@ function sortperm(A::AbstractArray;
11551151 end
11561152 end
11571153 ix = copymutable (LinearIndices (A))
1158- sort! (ix; alg, order = Perm (ordr, vec (A)), workspace , dims... )
1154+ sort! (ix; alg, order = Perm (ordr, vec (A)), buffer , dims... )
11591155end
11601156
11611157
@@ -1201,15 +1197,15 @@ function sortperm!(ix::AbstractArray{T}, A::AbstractArray;
12011197 rev:: Union{Bool,Nothing} = nothing ,
12021198 order:: Ordering = Forward,
12031199 initialized:: Bool = false ,
1204- workspace :: Union{AbstractVector{T}, Nothing} = nothing ,
1200+ buffer :: Union{AbstractVector{T}, Nothing} = nothing ,
12051201 dims... ) where T <: Integer # to optionally specify dims argument
12061202 (typeof (A) <: AbstractVector ) == (:dims in keys (dims)) && throw (ArgumentError (" Dims argument incorrect for type $(typeof (A)) " ))
12071203 axes (ix) == axes (A) || throw (ArgumentError (" index array must have the same size/axes as the source array, $(axes (ix)) != $(axes (A)) " ))
12081204
12091205 if ! initialized
12101206 ix .= LinearIndices (A)
12111207 end
1212- sort! (ix; alg, order = Perm (ord (lt, by, rev, order), vec (A)), workspace , dims... )
1208+ sort! (ix; alg, order = Perm (ord (lt, by, rev, order), vec (A)), buffer , dims... )
12131209end
12141210
12151211# sortperm for vectors of few unique integers
@@ -1274,19 +1270,19 @@ function sort(A::AbstractArray{T};
12741270 by= identity,
12751271 rev:: Union{Bool,Nothing} = nothing ,
12761272 order:: Ordering = Forward,
1277- workspace :: Union{AbstractVector{T}, Nothing} = similar (A, size (A, dims))) where T
1273+ buffer :: Union{AbstractVector{T}, Nothing} = similar (A, size (A, dims))) where T
12781274 dim = dims
12791275 order = ord (lt,by,rev,order)
12801276 n = length (axes (A, dim))
12811277 if dim != 1
12821278 pdims = (dim, setdiff (1 : ndims (A), dim)... ) # put the selected dimension first
12831279 Ap = permutedims (A, pdims)
12841280 Av = vec (Ap)
1285- sort_chunks! (Av, n, alg, order, workspace )
1281+ sort_chunks! (Av, n, alg, order, buffer )
12861282 permutedims (Ap, invperm (pdims))
12871283 else
12881284 Av = A[:]
1289- sort_chunks! (Av, n, alg, order, workspace )
1285+ sort_chunks! (Av, n, alg, order, buffer )
12901286 reshape (Av, axes (A))
12911287 end
12921288end
@@ -1335,21 +1331,21 @@ function sort!(A::AbstractArray{T};
13351331 by= identity,
13361332 rev:: Union{Bool,Nothing} = nothing ,
13371333 order:: Ordering = Forward,
1338- workspace :: Union{AbstractVector{T}, Nothing} = similar (A, size (A, dims))) where T
1339- _sort! (A, Val (dims), alg, ord (lt, by, rev, order), workspace )
1334+ buffer :: Union{AbstractVector{T}, Nothing} = similar (A, size (A, dims))) where T
1335+ _sort! (A, Val (dims), alg, ord (lt, by, rev, order), buffer )
13401336end
13411337function _sort! (A:: AbstractArray{T} , :: Val{K} ,
13421338 alg:: Algorithm ,
13431339 order:: Ordering ,
1344- workspace :: Union{AbstractVector{T}, Nothing} ) where {K,T}
1340+ buffer :: Union{AbstractVector{T}, Nothing} ) where {K,T}
13451341 nd = ndims (A)
13461342
13471343 1 <= K <= nd || throw (ArgumentError (" dimension out of range" ))
13481344
13491345 remdims = ntuple (i -> i == K ? 1 : axes (A, i), nd)
13501346 for idx in CartesianIndices (remdims)
13511347 Av = view (A, ntuple (i -> i == K ? Colon () : idx[i], nd)... )
1352- sort! (Av, alg, order, workspace )
1348+ sort! (Av, alg, order, buffer )
13531349 end
13541350 A
13551351end
0 commit comments