Skip to content

Commit

Permalink
deprecate lexcmp and lexless to cmp and isless
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 4, 2018
1 parent 2043060 commit c3a76c0
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 85 deletions.
9 changes: 6 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1556,14 +1556,17 @@ function isequal(A::AbstractArray, B::AbstractArray)
return true
end

function lexcmp(A::AbstractArray, B::AbstractArray)
function cmp(A::AbstractArray, B::AbstractArray)
for (a, b) in zip(A, B)
res = lexcmp(a, b)
res == 0 || return res
if !isequal(a, b)
return isless(a, b) ? -1 : 1
end
end
return cmp(length(A), length(B))
end

isless(A::AbstractArray, B::AbstractArray) = cmp(A, B) < 0

function (==)(A::AbstractArray, B::AbstractArray)
if axes(A) != axes(B)
return false
Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,8 @@ end

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

# use memcmp for lexcmp on byte arrays
function lexcmp(a::Array{UInt8,1}, b::Array{UInt8,1})
# use memcmp for cmp on byte arrays
function cmp(a::Array{UInt8,1}, b::Array{UInt8,1})
c = _memcmp(a, b, min(length(a),length(b)))
return c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
end
Expand Down
6 changes: 0 additions & 6 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,6 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
end
atanh(z::Complex) = atanh(float(z))

function lexcmp(a::Complex, b::Complex)
c = cmp(real(a), real(b))
c == 0 || return c
cmp(imag(a), imag(b))
end

#Rounding complex numbers
#Requires two different RoundingModes for the real and imaginary components
"""
Expand Down
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3806,6 +3806,14 @@ end
@deprecate getq(F::Factorization) F.Q
end

# issue #5290
@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
@deprecate lexcmp(x, y) cmp(x, y)

@deprecate lexless isless

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
2 changes: 0 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,6 @@ export
isimmutable,
isless,
ifelse,
lexless,
lexcmp,
object_id,
sizeof,

Expand Down
32 changes: 4 additions & 28 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ const ≥ = >=
# this definition allows Number types to implement < instead of isless,
# which is more idiomatic:
isless(x::Real, y::Real) = x<y
lexcmp(x::Real, y::Real) = isless(x,y) ? -1 : ifelse(isless(y,x), 1, 0)

"""
ifelse(condition::Bool, x, y)
Expand Down Expand Up @@ -342,35 +341,12 @@ Stacktrace:
cmp(x, y) = isless(x, y) ? -1 : ifelse(isless(y, x), 1, 0)

"""
lexcmp(x, y)
cmp(<, x, y)
Compare `x` and `y` lexicographically and return -1, 0, or 1 depending on whether `x` is
less than, equal to, or greater than `y`, respectively. This function should be defined for
lexicographically comparable types, and `lexless` will call `lexcmp` by default.
# Examples
```jldoctest
julia> lexcmp("abc", "abd")
-1
julia> lexcmp("abc", "abc")
0
```
"""
lexcmp(x, y) = cmp(x, y)

"""
lexless(x, y)
Determine whether `x` is lexicographically less than `y`.
# Examples
```jldoctest
julia> lexless("abc", "abd")
true
```
Return -1, 0, or 1 depending on whether `x` is less than, equal to, or greater than `y`,
respectively. The first argument specifies a less-than comparison function to use.
"""
lexless(x, y) = lexcmp(x,y) < 0
cmp(<, x, y) = (x < y) ? -1 : ifelse(y < x, 1, 0)

# cmp returns -1, 0, +1 indicating ordering
cmp(x::Integer, y::Integer) = ifelse(isless(x, y), -1, ifelse(isless(y, x), 1, 0))
Expand Down
12 changes: 2 additions & 10 deletions base/ordering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module Order
## notions of element ordering ##

export # not exported by Base
Ordering, Forward, Reverse, Lexicographic,
Ordering, Forward, Reverse,
By, Lt, Perm,
ReverseOrdering, ForwardOrdering, LexicographicOrdering,
ReverseOrdering, ForwardOrdering,
DirectOrdering,
lt, ord, ordtype

Expand All @@ -26,9 +26,6 @@ const DirectOrdering = Union{ForwardOrdering,ReverseOrdering{ForwardOrdering}}
const Forward = ForwardOrdering()
const Reverse = ReverseOrdering(Forward)

struct LexicographicOrdering <: Ordering end
const Lexicographic = LexicographicOrdering()

struct By{T} <: Ordering
by::T
end
Expand All @@ -46,17 +43,12 @@ lt(o::ForwardOrdering, a, b) = isless(a,b)
lt(o::ReverseOrdering, a, b) = lt(o.fwd,b,a)
lt(o::By, a, b) = isless(o.by(a),o.by(b))
lt(o::Lt, a, b) = o.lt(a,b)
lt(o::LexicographicOrdering, a, b) = lexcmp(a,b) < 0

Base.@propagate_inbounds function lt(p::Perm, a::Integer, b::Integer)
da = p.data[a]
db = p.data[b]
lt(p.order, da, db) | (!lt(p.order, db, da) & (a < b))
end
Base.@propagate_inbounds function lt(p::Perm{LexicographicOrdering}, a::Integer, b::Integer)
c = lexcmp(p.data[a], p.data[b])
c != 0 ? c < 0 : a < b
end

ordtype(o::ReverseOrdering, vs::AbstractArray) = ordtype(o.fwd, vs)
ordtype(o::Perm, vs::AbstractArray) = ordtype(o.order, o.data)
Expand Down
4 changes: 2 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ function sortrows(A::AbstractMatrix; kws...)
for i in inds
rows[i] = view(A, i, :)
end
p = sortperm(rows; kws..., order=Lexicographic)
p = sortperm(rows; kws...)
A[p,:]
end

Expand Down Expand Up @@ -966,7 +966,7 @@ function sortcols(A::AbstractMatrix; kws...)
for i in inds
cols[i] = view(A, :, i)
end
p = sortperm(cols; kws..., order=Lexicographic)
p = sortperm(cols; kws...)
A[:,p]
end

Expand Down
2 changes: 0 additions & 2 deletions doc/src/stdlib/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ Core.isa
Base.isequal
Base.isless
Base.ifelse
Base.lexcmp
Base.lexless
Core.typeassert
Core.typeof
Core.tuple
Expand Down
34 changes: 17 additions & 17 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1033,27 +1033,27 @@ end
end

@testset "lexicographic comparison" begin
@test lexcmp([1.0], [1]) == 0
@test lexcmp([1], [1.0]) == 0
@test lexcmp([1, 1], [1, 1]) == 0
@test lexcmp([1, 1], [2, 1]) == -1
@test lexcmp([2, 1], [1, 1]) == 1
@test lexcmp([1, 1], [1, 2]) == -1
@test lexcmp([1, 2], [1, 1]) == 1
@test lexcmp([1], [1, 1]) == -1
@test lexcmp([1, 1], [1]) == 1
@test cmp([1.0], [1]) == 0
@test cmp([1], [1.0]) == 0
@test cmp([1, 1], [1, 1]) == 0
@test cmp([1, 1], [2, 1]) == -1
@test cmp([2, 1], [1, 1]) == 1
@test cmp([1, 1], [1, 2]) == -1
@test cmp([1, 2], [1, 1]) == 1
@test cmp([1], [1, 1]) == -1
@test cmp([1, 1], [1]) == 1
end

@testset "sort on arrays" begin
local a = rand(3,3)

asr = sortrows(a)
@test lexless(asr[1,:],asr[2,:])
@test lexless(asr[2,:],asr[3,:])
@test isless(asr[1,:],asr[2,:])
@test isless(asr[2,:],asr[3,:])

asc = sortcols(a)
@test lexless(asc[:,1],asc[:,2])
@test lexless(asc[:,2],asc[:,3])
@test isless(asc[:,1],asc[:,2])
@test isless(asc[:,2],asc[:,3])

# mutating functions
o = ones(3, 4)
Expand All @@ -1062,12 +1062,12 @@ end
@test o == ones(3, 4)

asr = sortrows(a, rev=true)
@test lexless(asr[2,:],asr[1,:])
@test lexless(asr[3,:],asr[2,:])
@test isless(asr[2,:],asr[1,:])
@test isless(asr[3,:],asr[2,:])

asc = sortcols(a, rev=true)
@test lexless(asc[:,2],asc[:,1])
@test lexless(asc[:,3],asc[:,2])
@test isless(asc[:,2],asc[:,1])
@test isless(asc[:,3],asc[:,2])

as = sort(a, 1)
@test issorted(as[:,1])
Expand Down
6 changes: 0 additions & 6 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,6 @@ end
@test isequal(atan(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "lexcmp" begin
@test lexcmp(1.0-1.0im, 1.0+0.0im) == -1
@test lexcmp(0.0+0.0im, 0.0+0.0im) == 0
@test lexcmp(1.0-1.0im, 0.0+0.0im) == 1
end

# misc.

@test complex(1//2,1//3)^2 === complex(5//36, 1//3)
Expand Down
2 changes: 1 addition & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ for s in [map(first,V8); X8],
ss = s[i:j]
ss in X8 || push!(X8, ss)
end
sort!(X8, lt=lexless)
sort!(X8, lt=isless)
sort!(X8, by=length)

I8 = [(s,map(UInt16,s)) for s in X8]
Expand Down
10 changes: 5 additions & 5 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,11 @@ end
@test !isless( 0,-0.0)

@test isless(-0.0, 0.0f0)
@test lexcmp(-0.0, 0.0f0) == -1
@test lexcmp(0.0, -0.0f0) == 1
@test lexcmp(NaN, 1) == 1
@test lexcmp(1, NaN) == -1
@test lexcmp(NaN, NaN) == 0
@test cmp(isless, -0.0, 0.0f0) == -1
@test cmp(isless, 0.0, -0.0f0) == 1
@test cmp(isless, NaN, 1) == 1
@test cmp(isless, 1, NaN) == -1
@test cmp(isless, NaN, NaN) == 0
end
@testset "Float vs Integer comparison" begin
for x=-5:5, y=-5:5
Expand Down
2 changes: 1 addition & 1 deletion test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import Base.<
@test isequal(minmax(TO23094(2), TO23094(1))[1], TO23094(1))
@test isequal(minmax(TO23094(2), TO23094(1))[2], TO23094(2))

@test lexless('a','b')
@test isless('a','b')

@test 1 .!= 2
@test 1 .== 1
Expand Down

0 comments on commit c3a76c0

Please sign in to comment.