Skip to content

Commit 69f9c2e

Browse files
Merge pull request #25380 from JuliaLang/jb/lexcmp
deprecate `lexcmp`, `lexless`; define `cmp` in terms of `isless`
2 parents 3e4ab51 + d1d48b0 commit 69f9c2e

19 files changed

+65
-114
lines changed

base/abstractarray.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,14 +1556,17 @@ function isequal(A::AbstractArray, B::AbstractArray)
15561556
return true
15571557
end
15581558

1559-
function lexcmp(A::AbstractArray, B::AbstractArray)
1559+
function cmp(A::AbstractArray, B::AbstractArray)
15601560
for (a, b) in zip(A, B)
1561-
res = lexcmp(a, b)
1562-
res == 0 || return res
1561+
if !isequal(a, b)
1562+
return isless(a, b) ? -1 : 1
1563+
end
15631564
end
15641565
return cmp(length(A), length(B))
15651566
end
15661567

1568+
isless(A::AbstractArray, B::AbstractArray) = cmp(A, B) < 0
1569+
15671570
function (==)(A::AbstractArray, B::AbstractArray)
15681571
if axes(A) != axes(B)
15691572
return false

base/array.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,8 @@ end
13091309

13101310
_memcmp(a, b, len) = ccall(:memcmp, Int32, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), a, b, len) % Int
13111311

1312-
# use memcmp for lexcmp on byte arrays
1313-
function lexcmp(a::Array{UInt8,1}, b::Array{UInt8,1})
1312+
# use memcmp for cmp on byte arrays
1313+
function cmp(a::Array{UInt8,1}, b::Array{UInt8,1})
13141314
c = _memcmp(a, b, min(length(a),length(b)))
13151315
return c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
13161316
end

base/complex.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,12 +938,6 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
938938
end
939939
atanh(z::Complex) = atanh(float(z))
940940

941-
function lexcmp(a::Complex, b::Complex)
942-
c = cmp(real(a), real(b))
943-
c == 0 || return c
944-
cmp(imag(a), imag(b))
945-
end
946-
947941
#Rounding complex numbers
948942
#Requires two different RoundingModes for the real and imaginary components
949943
"""

base/deprecated.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,14 @@ end
38063806
@deprecate getq(F::Factorization) F.Q
38073807
end
38083808

3809+
# issue #5290
3810+
@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
3811+
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
3812+
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
3813+
@deprecate lexcmp(x, y) cmp(x, y)
3814+
3815+
@deprecate lexless isless
3816+
38093817
# END 0.7 deprecations
38103818

38113819
# BEGIN 1.0 deprecations

base/exports.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,6 @@ export
804804
isimmutable,
805805
isless,
806806
ifelse,
807-
lexless,
808-
lexcmp,
809807
object_id,
810808
sizeof,
811809

base/float.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -459,22 +459,6 @@ for op in (:<, :<=, :isless)
459459
@eval ($op)(a::Float16, b::Float16) = ($op)(Float32(a), Float32(b))
460460
end
461461

462-
function cmp(x::AbstractFloat, y::AbstractFloat)
463-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
464-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
465-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
466-
end
467-
468-
function cmp(x::Real, y::AbstractFloat)
469-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
470-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
471-
end
472-
473-
function cmp(x::AbstractFloat, y::Real)
474-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
475-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
476-
end
477-
478462
# Exact Float (Tf) vs Integer (Ti) comparisons
479463
# Assumes:
480464
# - typemax(Ti) == 2^n-1

base/gmp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ cmp(x::BigInt, y::CulongMax) = MPZ.cmp_ui(x, y)
492492
cmp(x::BigInt, y::Integer) = cmp(x, big(y))
493493
cmp(x::Integer, y::BigInt) = -cmp(y, x)
494494

495-
cmp(x::BigInt, y::CdoubleMax) = isnan(y) ? throw(DomainError(y, "`y` cannot be NaN.")) : MPZ.cmp_d(x, y)
495+
cmp(x::BigInt, y::CdoubleMax) = isnan(y) ? -1 : MPZ.cmp_d(x, y)
496496
cmp(x::CdoubleMax, y::BigInt) = -cmp(y, x)
497497

498498
isqrt(x::BigInt) = MPZ.sqrt(x)

base/mpfr.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,23 +676,23 @@ end
676676
>(x::BigFloat, y::BigFloat) = ccall((:mpfr_greater_p, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}), x, y) != 0
677677

678678
function cmp(x::BigFloat, y::BigInt)
679-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
679+
isnan(x) && return 1
680680
ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigInt}), x, y)
681681
end
682682
function cmp(x::BigFloat, y::ClongMax)
683-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
683+
isnan(x) && return 1
684684
ccall((:mpfr_cmp_si, :libmpfr), Int32, (Ref{BigFloat}, Clong), x, y)
685685
end
686686
function cmp(x::BigFloat, y::CulongMax)
687-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
687+
isnan(x) && return 1
688688
ccall((:mpfr_cmp_ui, :libmpfr), Int32, (Ref{BigFloat}, Culong), x, y)
689689
end
690690
cmp(x::BigFloat, y::Integer) = cmp(x,big(y))
691691
cmp(x::Integer, y::BigFloat) = -cmp(y,x)
692692

693693
function cmp(x::BigFloat, y::CdoubleMax)
694-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
695-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
694+
isnan(x) && return isnan(y) ? 0 : 1
695+
isnan(y) && return -1
696696
ccall((:mpfr_cmp_d, :libmpfr), Int32, (Ref{BigFloat}, Cdouble), x, y)
697697
end
698698
cmp(x::CdoubleMax, y::BigFloat) = -cmp(y,x)

base/operators.jl

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ const ≥ = >=
300300
# this definition allows Number types to implement < instead of isless,
301301
# which is more idiomatic:
302302
isless(x::Real, y::Real) = x<y
303-
lexcmp(x::Real, y::Real) = isless(x,y) ? -1 : ifelse(isless(y,x), 1, 0)
304303

305304
"""
306305
ifelse(condition::Bool, x, y)
@@ -342,35 +341,12 @@ Stacktrace:
342341
cmp(x, y) = isless(x, y) ? -1 : ifelse(isless(y, x), 1, 0)
343342

344343
"""
345-
lexcmp(x, y)
344+
cmp(<, x, y)
346345
347-
Compare `x` and `y` lexicographically and return -1, 0, or 1 depending on whether `x` is
348-
less than, equal to, or greater than `y`, respectively. This function should be defined for
349-
lexicographically comparable types, and `lexless` will call `lexcmp` by default.
350-
351-
# Examples
352-
```jldoctest
353-
julia> lexcmp("abc", "abd")
354-
-1
355-
356-
julia> lexcmp("abc", "abc")
357-
0
358-
```
359-
"""
360-
lexcmp(x, y) = cmp(x, y)
361-
362-
"""
363-
lexless(x, y)
364-
365-
Determine whether `x` is lexicographically less than `y`.
366-
367-
# Examples
368-
```jldoctest
369-
julia> lexless("abc", "abd")
370-
true
371-
```
346+
Return -1, 0, or 1 depending on whether `x` is less than, equal to, or greater than `y`,
347+
respectively. The first argument specifies a less-than comparison function to use.
372348
"""
373-
lexless(x, y) = lexcmp(x,y) < 0
349+
cmp(<, x, y) = (x < y) ? -1 : ifelse(y < x, 1, 0)
374350

375351
# cmp returns -1, 0, +1 indicating ordering
376352
cmp(x::Integer, y::Integer) = ifelse(isless(x, y), -1, ifelse(isless(y, x), 1, 0))

base/ordering.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module Order
55
## notions of element ordering ##
66

77
export # not exported by Base
8-
Ordering, Forward, Reverse, Lexicographic,
8+
Ordering, Forward, Reverse,
99
By, Lt, Perm,
10-
ReverseOrdering, ForwardOrdering, LexicographicOrdering,
10+
ReverseOrdering, ForwardOrdering,
1111
DirectOrdering,
1212
lt, ord, ordtype
1313

@@ -26,9 +26,6 @@ const DirectOrdering = Union{ForwardOrdering,ReverseOrdering{ForwardOrdering}}
2626
const Forward = ForwardOrdering()
2727
const Reverse = ReverseOrdering(Forward)
2828

29-
struct LexicographicOrdering <: Ordering end
30-
const Lexicographic = LexicographicOrdering()
31-
3229
struct By{T} <: Ordering
3330
by::T
3431
end
@@ -46,17 +43,12 @@ lt(o::ForwardOrdering, a, b) = isless(a,b)
4643
lt(o::ReverseOrdering, a, b) = lt(o.fwd,b,a)
4744
lt(o::By, a, b) = isless(o.by(a),o.by(b))
4845
lt(o::Lt, a, b) = o.lt(a,b)
49-
lt(o::LexicographicOrdering, a, b) = lexcmp(a,b) < 0
5046

5147
Base.@propagate_inbounds function lt(p::Perm, a::Integer, b::Integer)
5248
da = p.data[a]
5349
db = p.data[b]
5450
lt(p.order, da, db) | (!lt(p.order, db, da) & (a < b))
5551
end
56-
Base.@propagate_inbounds function lt(p::Perm{LexicographicOrdering}, a::Integer, b::Integer)
57-
c = lexcmp(p.data[a], p.data[b])
58-
c != 0 ? c < 0 : a < b
59-
end
6052

6153
ordtype(o::ReverseOrdering, vs::AbstractArray) = ordtype(o.fwd, vs)
6254
ordtype(o::Perm, vs::AbstractArray) = ordtype(o.order, o.data)

0 commit comments

Comments
 (0)