-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Range indexing: error with scalar bool index like all other arrays #31829
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
904e6a7
An initial proposal to check Bool case
bkamins 1cdfe4d
Update base/range.jl
bkamins 90e765a
Merge branch 'master' into fix_bool_getindex
bkamins 759e1c0
Update NEWS.md
bkamins 04aa80d
Update base/range.jl
bkamins 23ffd52
Update base/range.jl
bkamins f6ec984
Update ranges.jl
bkamins 1047017
Update test/ranges.jl
bkamins 6ad94de
Apply suggestions from code review
bkamins 39b5419
Update NEWS.md
bkamins 8a10ef7
Update NEWS.md
bkamins bd44211
do not introduce new methods
bkamins b67c55c
try to identify source of the problem with views
bkamins 3391dc7
Disallow OneTo(::Bool)
bkamins 427beb4
Update test/ranges.jl
bkamins 6aafb09
Update test/ranges.jl
bkamins c40212c
Update test/ranges.jl
bkamins e23cede
Update test/ranges.jl
bkamins aebe5f3
Fix OneTo
bkamins 2ae8710
Update base/range.jl
bkamins 55f2487
Apply suggestions from code review
bkamins 447caa7
Update base/range.jl
bkamins 59c8866
Merge branch 'master' into fix_bool_getindex
bkamins e040c25
Merge branch 'master' into fix_bool_getindex
bkamins 8e71434
add tests comparing to Array behaviors
mbauman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -406,12 +406,19 @@ be 1. | |||||||
""" | ||||||||
struct OneTo{T<:Integer} <: AbstractUnitRange{T} | ||||||||
stop::T | ||||||||
OneTo{T}(stop) where {T<:Integer} = new(max(zero(T), stop)) | ||||||||
function OneTo{T}(stop) where {T<:Integer} | ||||||||
throwbool(r) = (@_noinline_meta; throw(ArgumentError("invalid index: $r of type Bool"))) | ||||||||
T === Bool && throwbool(stop) | ||||||||
return new(max(zero(T), stop)) | ||||||||
end | ||||||||
|
||||||||
function OneTo{T}(r::AbstractRange) where {T<:Integer} | ||||||||
throwstart(r) = (@_noinline_meta; throw(ArgumentError("first element must be 1, got $(first(r))"))) | ||||||||
throwstep(r) = (@_noinline_meta; throw(ArgumentError("step must be 1, got $(step(r))"))) | ||||||||
throwbool(r) = (@_noinline_meta; throw(ArgumentError("invalid index: $r of type Bool"))) | ||||||||
first(r) == 1 || throwstart(r) | ||||||||
step(r) == 1 || throwstep(r) | ||||||||
T === Bool && throwbool(r) | ||||||||
return new(max(zero(T), last(r))) | ||||||||
end | ||||||||
end | ||||||||
|
@@ -762,6 +769,7 @@ _in_unit_range(v::UnitRange, val, i::Integer) = i > 0 && val <= v.stop && val >= | |||||||
|
||||||||
function getindex(v::UnitRange{T}, i::Integer) where T | ||||||||
@_inline_meta | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
val = convert(T, v.start + (i - 1)) | ||||||||
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i) | ||||||||
val | ||||||||
|
@@ -772,19 +780,22 @@ const OverflowSafe = Union{Bool,Int8,Int16,Int32,Int64,Int128, | |||||||
|
||||||||
function getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe} | ||||||||
@_inline_meta | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
val = v.start + (i - 1) | ||||||||
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i) | ||||||||
val % T | ||||||||
end | ||||||||
|
||||||||
function getindex(v::OneTo{T}, i::Integer) where T | ||||||||
@_inline_meta | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
@boundscheck ((i > 0) & (i <= v.stop)) || throw_boundserror(v, i) | ||||||||
convert(T, i) | ||||||||
end | ||||||||
|
||||||||
function getindex(v::AbstractRange{T}, i::Integer) where T | ||||||||
@_inline_meta | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
ret = convert(T, first(v) + (i - 1)*step_hp(v)) | ||||||||
ok = ifelse(step(v) > zero(step(v)), | ||||||||
(ret <= last(v)) & (ret >= first(v)), | ||||||||
|
@@ -795,22 +806,26 @@ end | |||||||
|
||||||||
function getindex(r::Union{StepRangeLen,LinRange}, i::Integer) | ||||||||
@_inline_meta | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
@boundscheck checkbounds(r, i) | ||||||||
unsafe_getindex(r, i) | ||||||||
end | ||||||||
|
||||||||
# This is separate to make it useful even when running with --check-bounds=yes | ||||||||
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
u = i - r.offset | ||||||||
T(r.ref + u*r.step) | ||||||||
end | ||||||||
|
||||||||
function _getindex_hiprec(r::StepRangeLen, i::Integer) # without rounding by T | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
u = i - r.offset | ||||||||
r.ref + u*r.step | ||||||||
end | ||||||||
|
||||||||
function unsafe_getindex(r::LinRange, i::Integer) | ||||||||
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool")) | ||||||||
lerpi(i-1, r.lendiv, r.start, r.stop) | ||||||||
end | ||||||||
|
||||||||
|
@@ -822,12 +837,27 @@ end | |||||||
|
||||||||
getindex(r::AbstractRange, ::Colon) = copy(r) | ||||||||
|
||||||||
function getindex(r::AbstractUnitRange, s::AbstractUnitRange{<:Integer}) | ||||||||
function getindex(r::AbstractUnitRange, s::AbstractUnitRange{T}) where {T<:Integer} | ||||||||
@_inline_meta | ||||||||
@boundscheck checkbounds(r, s) | ||||||||
f = first(r) | ||||||||
st = oftype(f, f + first(s)-1) | ||||||||
range(st, length=length(s)) | ||||||||
|
||||||||
if T === Bool | ||||||||
if length(s) == 0 | ||||||||
return r | ||||||||
elseif length(s) == 1 | ||||||||
if first(s) | ||||||||
return r | ||||||||
else | ||||||||
return range(r[1], length=0) | ||||||||
end | ||||||||
else # length(s) == 2 | ||||||||
return range(r[2], length=1) | ||||||||
end | ||||||||
else | ||||||||
f = first(r) | ||||||||
st = oftype(f, f + first(s)-1) | ||||||||
return range(st, length=length(s)) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
function getindex(r::OneTo{T}, s::OneTo) where T | ||||||||
|
@@ -836,36 +866,96 @@ function getindex(r::OneTo{T}, s::OneTo) where T | |||||||
OneTo(T(s.stop)) | ||||||||
end | ||||||||
|
||||||||
function getindex(r::AbstractUnitRange, s::StepRange{<:Integer}) | ||||||||
function getindex(r::AbstractUnitRange, s::StepRange{T}) where {T<:Integer} | ||||||||
@_inline_meta | ||||||||
@boundscheck checkbounds(r, s) | ||||||||
st = oftype(first(r), first(r) + s.start-1) | ||||||||
range(st, step=step(s), length=length(s)) | ||||||||
|
||||||||
if T === Bool | ||||||||
if length(s) == 0 | ||||||||
return range(first(r), step=one(eltype(r)), length=0) | ||||||||
elseif length(s) == 1 | ||||||||
if first(s) | ||||||||
return range(first(r), step=one(eltype(r)), length=1) | ||||||||
else | ||||||||
return range(first(r), step=one(eltype(r)), length=0) | ||||||||
end | ||||||||
else # length(s) == 2 | ||||||||
return range(r[2], step=one(eltype(r)), length=1) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
end | ||||||||
else | ||||||||
st = oftype(first(r), first(r) + s.start-1) | ||||||||
return range(st, step=step(s), length=length(s)) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
function getindex(r::StepRange, s::AbstractRange{<:Integer}) | ||||||||
function getindex(r::StepRange, s::AbstractRange{T}) where {T<:Integer} | ||||||||
@_inline_meta | ||||||||
@boundscheck checkbounds(r, s) | ||||||||
st = oftype(r.start, r.start + (first(s)-1)*step(r)) | ||||||||
range(st, step=step(r)*step(s), length=length(s)) | ||||||||
|
||||||||
if T === Bool | ||||||||
if length(s) == 0 | ||||||||
return range(first(r), step=step(r), length=0) | ||||||||
elseif length(s) == 1 | ||||||||
if first(s) | ||||||||
return range(first(r), step=step(r), length=1) | ||||||||
else | ||||||||
return range(first(r), step=step(r), length=0) | ||||||||
end | ||||||||
else # length(s) == 2 | ||||||||
return range(r[2], step=step(r), length=1) | ||||||||
end | ||||||||
else | ||||||||
st = oftype(r.start, r.start + (first(s)-1)*step(r)) | ||||||||
return range(st, step=step(r)*step(s), length=length(s)) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T} | ||||||||
function getindex(r::StepRangeLen{T}, s::OrdinalRange{S}) where {T, S<:Integer} | ||||||||
@_inline_meta | ||||||||
@boundscheck checkbounds(r, s) | ||||||||
# Find closest approach to offset by s | ||||||||
ind = LinearIndices(s) | ||||||||
offset = max(min(1 + round(Int, (r.offset - first(s))/step(s)), last(ind)), first(ind)) | ||||||||
ref = _getindex_hiprec(r, first(s) + (offset-1)*step(s)) | ||||||||
return StepRangeLen{T}(ref, r.step*step(s), length(s), offset) | ||||||||
|
||||||||
if S === Bool | ||||||||
if length(s) == 0 | ||||||||
return StepRangeLen{T}(first(r), step(r), 0, 1) | ||||||||
elseif length(s) == 1 | ||||||||
if first(s) | ||||||||
return StepRangeLen{T}(first(r), step(r), 1, 1) | ||||||||
else | ||||||||
return StepRangeLen{T}(first(r), step(r), 0, 1) | ||||||||
end | ||||||||
else # length(s) == 2 | ||||||||
return StepRangeLen{T}(r[2], step(r), 1, 1) | ||||||||
end | ||||||||
else | ||||||||
# Find closest approach to offset by s | ||||||||
ind = LinearIndices(s) | ||||||||
offset = max(min(1 + round(Int, (r.offset - first(s))/step(s)), last(ind)), first(ind)) | ||||||||
ref = _getindex_hiprec(r, first(s) + (offset-1)*step(s)) | ||||||||
return StepRangeLen{T}(ref, r.step*step(s), length(s), offset) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
function getindex(r::LinRange{T}, s::OrdinalRange{<:Integer}) where {T} | ||||||||
function getindex(r::LinRange{T}, s::OrdinalRange{S}) where {T, S<:Integer} | ||||||||
@_inline_meta | ||||||||
@boundscheck checkbounds(r, s) | ||||||||
vfirst = unsafe_getindex(r, first(s)) | ||||||||
vlast = unsafe_getindex(r, last(s)) | ||||||||
return LinRange{T}(vfirst, vlast, length(s)) | ||||||||
|
||||||||
if S === Bool | ||||||||
if length(s) == 0 | ||||||||
return LinRange(first(r), first(r), 0) | ||||||||
elseif length(s) == 1 | ||||||||
if first(s) | ||||||||
return LinRange(first(r), first(r), 1) | ||||||||
else | ||||||||
return LinRange(first(r), first(r), 0) | ||||||||
end | ||||||||
else # length(s) == 2 | ||||||||
return LinRange(r[2], r[2], 1) | ||||||||
end | ||||||||
else | ||||||||
vfirst = unsafe_getindex(r, first(s)) | ||||||||
vlast = unsafe_getindex(r, last(s)) | ||||||||
return LinRange{T}(vfirst, vlast, length(s)) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r))) | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case someone makes an OffsetUnitRange?