Skip to content

Commit f200489

Browse files
committed
Deprecate sort for NTuple and other iterables
1 parent 5dd96a5 commit f200489

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ changes in `julia`.
7272

7373
* `@compat public foo, bar` marks `foo` and `bar` as public in Julia 1.11+ and is a no-op in Julia 1.10 and earlier. ([#50105]) (since Compat 4.10.0)
7474

75-
* `sort` for `NTuple` and other iterables. ([#46104]) (since Compat 4.9.0)
76-
7775
* `redirect_stdio`, for simple stream redirection. ([#37978]) (since Compat 4.8.0)
7876

7977
* `trunc`, `floor`, `ceil`, and `round` to `Bool`. ([#25085]) (since Compat 4.7.0)

src/Compat.jl

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -705,42 +705,6 @@ if VERSION < v"1.7.0-DEV.1187"
705705
export redirect_stdio
706706
end
707707

708-
# https://github.com/JuliaLang/julia/pull/46104
709-
if VERSION < v"1.10.0-DEV.1404"
710-
using Base: Ordering, Forward, ord, lt, tail, copymutable, DEFAULT_STABLE, IteratorSize, HasShape, IsInfinite
711-
function Base.sort(v; kws...)
712-
size = IteratorSize(v)
713-
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
714-
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
715-
sort!(copymutable(v); kws...)
716-
end
717-
Base.sort(::AbstractString; kws...) =
718-
throw(ArgumentError("sort(::AbstractString) is not supported"))
719-
Base.sort(::Tuple; kws...) =
720-
throw(ArgumentError("sort(::Tuple) is only supported for NTuples"))
721-
722-
function Base.sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
723-
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
724-
o = ord(lt,by,rev,order)
725-
if N > 9
726-
v = sort!(copymutable(x), DEFAULT_STABLE, o)
727-
tuple((v[i] for i in 1:N)...)
728-
else
729-
_sort(x, o)
730-
end
731-
end
732-
_sort(x::Union{NTuple{0}, NTuple{1}}, o::Ordering) = x
733-
function _sort(x::NTuple, o::Ordering)
734-
a, b = Base.IteratorsMD.split(x, Val(length(x)>>1))
735-
merge(_sort(a, o), _sort(b, o), o)
736-
end
737-
merge(x::NTuple, y::NTuple{0}, o::Ordering) = x
738-
merge(x::NTuple{0}, y::NTuple, o::Ordering) = y
739-
merge(x::NTuple{0}, y::NTuple{0}, o::Ordering) = x # Method ambiguity
740-
merge(x::NTuple, y::NTuple, o::Ordering) =
741-
(lt(o, y[1], x[1]) ? (y[1], merge(x, tail(y), o)...) : (x[1], merge(tail(x), y, o)...))
742-
end
743-
744708
include("deprecated.jl")
745709

746710
end # module Compat

src/deprecated.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,48 @@ Base.@deprecate_binding parseatom Meta.parseatom false
1515
Base.@deprecate_binding parseall Meta.parseall false
1616
import UUIDs
1717
Base.@deprecate_binding uuid5 UUIDs.uuid5 true
18+
19+
# https://github.com/JuliaLang/julia/pull/46104
20+
# reverted in https://github.com/JuliaLang/julia/pull/52010
21+
if VERSION < v"1.10.0-DEV.1404" ||
22+
(VERSION >= v"1.10-rc2" && VERSION < v"1.11.0-") ||
23+
VERSION >= v"1.11.0-DEV.924"
24+
using Base: Ordering, Forward, ord, lt, tail, copymutable, DEFAULT_STABLE, IteratorSize, HasShape, IsInfinite
25+
function Base.sort(v; kws...)
26+
Base.depwarn("sorting arbitrary iterables is deprecated", :sort)
27+
if v isa AbstractString
28+
throw(ArgumentError("sort(::AbstractString) is not supported"))
29+
end
30+
if v isa NTuple
31+
return _sort(v; kws...)
32+
end
33+
if v isa Tuple
34+
throw(ArgumentError("sort(::Tuple) is only supported for NTuples"))
35+
end
36+
size = IteratorSize(v)
37+
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
38+
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
39+
sort!(copymutable(v); kws...)
40+
end
41+
42+
function _sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
43+
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
44+
o = ord(lt,by,rev,order)
45+
if N > 9
46+
v = sort!(copymutable(x), DEFAULT_STABLE, o)
47+
tuple((v[i] for i in 1:N)...)
48+
else
49+
_sort(x, o)
50+
end
51+
end
52+
_sort(x::Union{NTuple{0}, NTuple{1}}, o::Ordering) = x
53+
function _sort(x::NTuple, o::Ordering)
54+
a, b = Base.IteratorsMD.split(x, Val(length(x)>>1))
55+
merge(_sort(a, o), _sort(b, o), o)
56+
end
57+
merge(x::NTuple, y::NTuple{0}, o::Ordering) = x
58+
merge(x::NTuple{0}, y::NTuple, o::Ordering) = y
59+
merge(x::NTuple{0}, y::NTuple{0}, o::Ordering) = x # Method ambiguity
60+
merge(x::NTuple, y::NTuple, o::Ordering) =
61+
(lt(o, y[1], x[1]) ? (y[1], merge(x, tail(y), o)...) : (x[1], merge(tail(x), y, o)...))
62+
end

test/runtests.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,6 @@ end
696696
@testset "sort(iterable)" begin
697697
function tuple_sort_test(x)
698698
@test issorted(sort(x))
699-
length(x) > 9 && return # length > 9 uses a vector fallback
700-
@test 0 == @allocated sort(x)
701699
end
702700
@testset "sort(::NTuple)" begin
703701
@test sort((9,8,3,3,6,2,0,8)) == (0,2,3,3,6,8,8,9)

0 commit comments

Comments
 (0)