From 37101e86131a212c78b49b5cbaaf219726f4d9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Farneb=C3=83=C2=A4ck?= Date: Wed, 15 May 2013 21:31:25 +0200 Subject: [PATCH] Revise issorted and contains to support ranges with step 0. --- base/range.jl | 4 ++-- test/corelib.jl | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index b7112e71077aa..8da423a6aef9e 100644 --- a/base/range.jl +++ b/base/range.jl @@ -344,7 +344,7 @@ reverse{T<:Real}(r::Ranges{T}) = Range(last(r), -step(r), r.len) ## sorting ## issorted(r::Range1) = true -issorted(r::Ranges) = step(r) > 0 +issorted(r::Ranges) = step(r) >= 0 sort(r::Range1) = r sort!(r::Range1) = r @@ -368,6 +368,6 @@ end map(f, r::Ranges) = [ f(x) for x in r ] function contains(r::Ranges, x) - n = ifloor((x-first(r))/step(r))+1 + n = step(r) == 0 ? 1 : ifloor((x-first(r))/step(r))+1 n >= 1 && n <= length(r) && r[n] == x end diff --git a/test/corelib.jl b/test/corelib.jl index cf7149be06295..5e6854ac5a3cf 100644 --- a/test/corelib.jl +++ b/test/corelib.jl @@ -82,6 +82,12 @@ end @test intersect(-10:3:24, -10:3:24) == -10:3:23 @test isempty(intersect(-11:3:24, -10:3:24)) +@test !contains(1:5, 3.5) +@test contains(1:5, 3) +@test contains(5:-1:1, 3) +@test contains(3+0*(1:5), 3) +@test !contains(3+0*(1:5), 4) + # comprehensions X = [ i+2j for i=1:5, j=1:5 ] @test X[2,3] == 8