-
Notifications
You must be signed in to change notification settings - Fork 59
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
safer indexing for AbstractArray #540
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,15 +114,15 @@ end | |
function rcopy(::Type{Vector{Bool}},s::Ptr{LglSxp}) | ||
a = Array{Bool}(undef, length(s)) | ||
v = unsafe_vec(s) | ||
for i = 1:length(a) | ||
for i in eachindex(a, v) | ||
a[i] = v[i] != 0 | ||
end | ||
a | ||
end | ||
function rcopy(::Type{BitVector},s::Ptr{LglSxp}) | ||
a = BitArray(undef, length(s)) | ||
v = unsafe_vec(s) | ||
for i = 1:length(a) | ||
for i in eachindex(a, v) | ||
a[i] = v[i] != 0 | ||
end | ||
a | ||
|
@@ -135,15 +135,15 @@ end | |
function rcopy(::Type{Array{Bool}},s::Ptr{LglSxp}) | ||
a = Array{Bool}(undef, size(s)...) | ||
v = unsafe_vec(s) | ||
for i = 1:length(a) | ||
for i in eachindex(a, v) | ||
a[i] = v[i] != 0 | ||
end | ||
a | ||
end | ||
function rcopy(::Type{BitArray},s::Ptr{LglSxp}) | ||
a = BitArray(undef, size(s)...) | ||
v = unsafe_vec(s) | ||
for i = 1:length(a) | ||
for i in eachindex(a, v) | ||
a[i] = v[i] != 0 | ||
end | ||
a | ||
|
@@ -264,8 +264,10 @@ sexp(::Type{RClass{:character}},st::AbstractString) = sexp(RClass{:character}, s | |
function sexp(::Type{RClass{:character}}, a::AbstractArray{T}) where T<:AbstractString | ||
ra = protect(allocArray(StrSxp, size(a)...)) | ||
try | ||
for i in 1:length(a) | ||
ra[i] = a[i] | ||
# we want this to work even if a doesn't use one-based indexing | ||
# we only care about ra having the same length (which it does) | ||
for (i, idx) in zip(eachindex(ra), eachindex(a)) | ||
ra[i] = a[idx] | ||
end | ||
finally | ||
unprotect(1) | ||
|
@@ -296,8 +298,10 @@ end | |
function sexp(::Type{RClass{:list}}, a::AbstractArray) | ||
ra = protect(allocArray(VecSxp, size(a)...)) | ||
try | ||
for i in 1:length(a) | ||
ra[i] = a[i] | ||
# we want this to work even if a doesn't use one-based indexing | ||
# we only care about ra having the same length (which it does) | ||
for (i, idx) in zip(eachindex(ra), eachindex(a)) | ||
ra[i] = a[idx] | ||
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. This feels like a situation where a 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. Hmmm, yeah, but I'm going to punt on that until I've tackled #444 |
||
end | ||
finally | ||
unprotect(1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ function iterate(s::Ptr{S}, state) where S<:VectorSxp | |
(s[state], state) | ||
end | ||
|
||
Base.eachindex(s::Ptr{<:VectorSxp}) = Base.OneTo(length(s)) | ||
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. Feels weird to define 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. yeah, it does, but then again we're treating R native arrays as iterators without copying... |
||
|
||
""" | ||
Set element of a VectorSxp by a label. | ||
|
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.
Note that this still assumes 1-based indexing for
ra
sinceenumerate
will start with 1.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.
yes, but we create
ra
and it's indeed one basedThere 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.
Fair, though we also create
a
andv
as 1-based arrays in the abovercopy
methods which means the loop specifications there were safe prior to usingeachindex
. I assumed you were going for minimal assumptions but if not then that's fine.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.
typeof(ra)
is lacking aeachindex
method and the fallback callskeys
; I'm going to take a stab at defining something appopriate