Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to StepRange: Take 2 #6700

Merged
merged 2 commits into from
Jun 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just override %?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I just looked a little closer at your implementation in Dates.


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 @@ -506,15 +508,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 Down Expand Up @@ -549,7 +551,7 @@ function map(f::Callable, 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