Skip to content

Commit

Permalink
Revise issorted and contains to support ranges with step 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarFarneback committed May 15, 2013
1 parent 82b6419 commit 37101e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 37101e8

Please sign in to comment.