Skip to content

Commit a768d0c

Browse files
LilithHafnerLilith HafnerKristofferC
authored
rename buffer to scratch in sorting (#47172)
Co-authored-by: Lilith Hafner <[email protected]> Co-authored-by: Kristoffer Carlsson <[email protected]>
1 parent 27859f3 commit a768d0c

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

base/sort.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ end
727727

728728
# For AbstractVector{Bool}, counting sort is always best.
729729
# This is an implementation of counting sort specialized for Bools.
730-
# Accepts unused buffer to avoid method ambiguity.
730+
# Accepts unused scratch space to avoid method ambiguity.
731731
function sort!(v::AbstractVector{Bool}, lo::Integer, hi::Integer, ::AdaptiveSortAlg, o::Ordering,
732732
t::Union{AbstractVector{Bool}, Nothing}=nothing)
733733
first = lt(o, false, true) ? false : lt(o, true, false) ? true : return v
@@ -856,15 +856,15 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, ::AdaptiveSortAlg
856856
end
857857

858858
len = lenm1 + 1
859-
if t !== nothing && checkbounds(Bool, t, lo:hi) # Fully preallocated and aligned buffer
859+
if t !== nothing && checkbounds(Bool, t, lo:hi) # Fully preallocated and aligned scratch space
860860
u2 = radix_sort!(u, lo, hi, bits, reinterpret(U, t))
861861
uint_unmap!(v, u2, lo, hi, o, u_min)
862-
elseif t !== nothing && (applicable(resize!, t, len) || length(t) >= len) # Viable buffer
862+
elseif t !== nothing && (applicable(resize!, t, len) || length(t) >= len) # Viable scratch space
863863
length(t) >= len || resize!(t, len)
864864
t1 = axes(t, 1) isa OneTo ? t : view(t, firstindex(t):lastindex(t))
865865
u2 = radix_sort!(view(u, lo:hi), 1, len, bits, reinterpret(U, t1))
866866
uint_unmap!(view(v, lo:hi), u2, 1, len, o, u_min)
867-
else # No viable buffer
867+
else # No viable scratch space
868868
u2 = radix_sort!(u, lo, hi, bits, similar(u))
869869
uint_unmap!(v, u2, lo, hi, o, u_min)
870870
end
@@ -930,8 +930,8 @@ function sort!(v::AbstractVector{T};
930930
by=identity,
931931
rev::Union{Bool,Nothing}=nothing,
932932
order::Ordering=Forward,
933-
buffer::Union{AbstractVector{T}, Nothing}=nothing) where T
934-
sort!(v, alg, ord(lt,by,rev,order), buffer)
933+
scratch::Union{AbstractVector{T}, Nothing}=nothing) where T
934+
sort!(v, alg, ord(lt,by,rev,order), scratch)
935935
end
936936

937937
# sort! for vectors of few unique integers
@@ -1070,7 +1070,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
10701070
order::Ordering=Forward,
10711071
initialized::Bool=false)
10721072
if axes(ix,1) != axes(v,1)
1073-
throw(ArgumentError("The index vector is used as a buffer and must have the " *
1073+
throw(ArgumentError("The index vector is used as scratch space and must have the " *
10741074
"same length/indices as the source vector, $(axes(ix,1)) != $(axes(v,1))"))
10751075
end
10761076
if !initialized
@@ -1137,7 +1137,7 @@ function sortperm(A::AbstractArray;
11371137
by=identity,
11381138
rev::Union{Bool,Nothing}=nothing,
11391139
order::Ordering=Forward,
1140-
buffer::Union{AbstractVector{<:Integer}, Nothing}=nothing,
1140+
scratch::Union{AbstractVector{<:Integer}, Nothing}=nothing,
11411141
dims...) #to optionally specify dims argument
11421142
ordr = ord(lt,by,rev,order)
11431143
if ordr === Forward && isa(A,Vector) && eltype(A)<:Integer
@@ -1152,7 +1152,7 @@ function sortperm(A::AbstractArray;
11521152
end
11531153
end
11541154
ix = copymutable(LinearIndices(A))
1155-
sort!(ix; alg, order = Perm(ordr, vec(A)), buffer, dims...)
1155+
sort!(ix; alg, order = Perm(ordr, vec(A)), scratch, dims...)
11561156
end
11571157

11581158

@@ -1198,15 +1198,15 @@ function sortperm!(ix::AbstractArray{T}, A::AbstractArray;
11981198
rev::Union{Bool,Nothing}=nothing,
11991199
order::Ordering=Forward,
12001200
initialized::Bool=false,
1201-
buffer::Union{AbstractVector{T}, Nothing}=nothing,
1201+
scratch::Union{AbstractVector{T}, Nothing}=nothing,
12021202
dims...) where T <: Integer #to optionally specify dims argument
12031203
(typeof(A) <: AbstractVector) == (:dims in keys(dims)) && throw(ArgumentError("Dims argument incorrect for type $(typeof(A))"))
12041204
axes(ix) == axes(A) || throw(ArgumentError("index array must have the same size/axes as the source array, $(axes(ix)) != $(axes(A))"))
12051205

12061206
if !initialized
12071207
ix .= LinearIndices(A)
12081208
end
1209-
sort!(ix; alg, order = Perm(ord(lt, by, rev, order), vec(A)), buffer, dims...)
1209+
sort!(ix; alg, order = Perm(ord(lt, by, rev, order), vec(A)), scratch, dims...)
12101210
end
12111211

12121212
# sortperm for vectors of few unique integers
@@ -1271,19 +1271,19 @@ function sort(A::AbstractArray{T};
12711271
by=identity,
12721272
rev::Union{Bool,Nothing}=nothing,
12731273
order::Ordering=Forward,
1274-
buffer::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1274+
scratch::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
12751275
dim = dims
12761276
order = ord(lt,by,rev,order)
12771277
n = length(axes(A, dim))
12781278
if dim != 1
12791279
pdims = (dim, setdiff(1:ndims(A), dim)...) # put the selected dimension first
12801280
Ap = permutedims(A, pdims)
12811281
Av = vec(Ap)
1282-
sort_chunks!(Av, n, alg, order, buffer)
1282+
sort_chunks!(Av, n, alg, order, scratch)
12831283
permutedims(Ap, invperm(pdims))
12841284
else
12851285
Av = A[:]
1286-
sort_chunks!(Av, n, alg, order, buffer)
1286+
sort_chunks!(Av, n, alg, order, scratch)
12871287
reshape(Av, axes(A))
12881288
end
12891289
end
@@ -1332,21 +1332,21 @@ function sort!(A::AbstractArray{T};
13321332
by=identity,
13331333
rev::Union{Bool,Nothing}=nothing,
13341334
order::Ordering=Forward,
1335-
buffer::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1336-
_sort!(A, Val(dims), alg, ord(lt, by, rev, order), buffer)
1335+
scratch::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1336+
_sort!(A, Val(dims), alg, ord(lt, by, rev, order), scratch)
13371337
end
13381338
function _sort!(A::AbstractArray{T}, ::Val{K},
13391339
alg::Algorithm,
13401340
order::Ordering,
1341-
buffer::Union{AbstractVector{T}, Nothing}) where {K,T}
1341+
scratch::Union{AbstractVector{T}, Nothing}) where {K,T}
13421342
nd = ndims(A)
13431343

13441344
1 <= K <= nd || throw(ArgumentError("dimension out of range"))
13451345

13461346
remdims = ntuple(i -> i == K ? 1 : axes(A, i), nd)
13471347
for idx in CartesianIndices(remdims)
13481348
Av = view(A, ntuple(i -> i == K ? Colon() : idx[i], nd)...)
1349-
sort!(Av, alg, order, buffer)
1349+
sort!(Av, alg, order, scratch)
13501350
end
13511351
A
13521352
end

test/sorting.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,19 +740,19 @@ end
740740
end
741741

742742
# This testset is at the end of the file because it is slow
743-
@testset "sort(x; buffer)" begin
743+
@testset "sort(x; scratch)" begin
744744
for n in [1,10,100,1000]
745745
v = rand(n)
746-
buffer = [0.0]
747-
@test sort(v) == sort(v; buffer)
748-
@test sort!(copy(v)) == sort!(copy(v); buffer)
749-
@test sortperm(v) == sortperm(v; buffer=[4])
750-
@test sortperm!(Vector{Int}(undef, n), v) == sortperm!(Vector{Int}(undef, n), v; buffer=[4])
746+
scratch = [0.0]
747+
@test sort(v) == sort(v; scratch)
748+
@test sort!(copy(v)) == sort!(copy(v); scratch)
749+
@test sortperm(v) == sortperm(v; scratch=[4])
750+
@test sortperm!(Vector{Int}(undef, n), v) == sortperm!(Vector{Int}(undef, n), v; scratch=[4])
751751

752752
n > 100 && continue
753753
M = rand(n, n)
754-
@test sort(M; dims=2) == sort(M; dims=2, buffer)
755-
@test sort!(copy(M); dims=1) == sort!(copy(M); dims=1, buffer)
754+
@test sort(M; dims=2) == sort(M; dims=2, scratch)
755+
@test sort!(copy(M); dims=1) == sort!(copy(M); dims=1, scratch)
756756
end
757757
end
758758

0 commit comments

Comments
 (0)