Skip to content

Commit

Permalink
Merge pull request #6700 from karbarcca/patch-3
Browse files Browse the repository at this point in the history
Tweaks to StepRange: Take 2
  • Loading branch information
JeffBezanson committed Jun 2, 2014
2 parents 57a8d54 + 7e63edc commit 0c3de45
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ immutable StepRange{T,S} <: OrdinalRange{T,S}
remain = oftype(T, unsigned(diff) % step)
end
else
remain = diff % step
remain = steprem(start,stop,step)
end
last = stop - remain
end
Expand All @@ -54,6 +54,8 @@ immutable StepRange{T,S} <: OrdinalRange{T,S}
end
end

steprem(start,stop,step) = (stop-start) % step

StepRange{T,S}(start::T, step::S, stop::T) = StepRange{T,S}(start, step, stop)

immutable UnitRange{T<:Real} <: OrdinalRange{T,Int}
Expand Down Expand Up @@ -491,15 +493,15 @@ reverse(r::FloatRange) = FloatRange(last(r), -r.step, r.len, r.divisor)
## sorting ##

issorted(r::UnitRange) = true
issorted(r::Range) = step(r) >= 0
issorted(r::Range) = step(r) >= zero(step(r))

sort(r::UnitRange) = r
sort!(r::UnitRange) = r

sort{T<:Real}(r::Range{T}) = issorted(r) ? r : reverse(r)
sort(r::Range) = issorted(r) ? r : reverse(r)

sortperm(r::UnitRange) = 1:length(r)
sortperm{T<:Real}(r::Range{T}) = issorted(r) ? (1:1:length(r)) : (length(r):-1:1)
sortperm(r::Range) = issorted(r) ? (1:1:length(r)) : (length(r):-1:1)

function sum{T<:Real}(r::Range{T})
l = length(r)
Expand All @@ -515,7 +517,7 @@ function map!(f::Callable, dest, r::Range)
end

function in(x, r::Range)
n = step(r) == 0 ? 1 : iround((x-first(r))/step(r))+1
n = step(r) == zero(step(r)) ? 1 : iround((x-first(r))/step(r))+1
n >= 1 && n <= length(r) && r[n] == x
end

Expand Down

0 comments on commit 0c3de45

Please sign in to comment.