From f7aa5f33445d720ea804929b5babc9fee76c98b0 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Fri, 19 Jul 2024 11:34:12 +0200 Subject: [PATCH 1/3] safer indexing for AbstractArray --- src/convert/axisarray.jl | 2 +- src/convert/base.jl | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/convert/axisarray.jl b/src/convert/axisarray.jl index cb147c5e..bc7e9f5e 100644 --- a/src/convert/axisarray.jl +++ b/src/convert/axisarray.jl @@ -4,7 +4,7 @@ function rcopy(::Type{AxisArray}, r::Ptr{S}) where {S<:VectorSxp} dnames = getattrib(r, Const.DimNamesSymbol) isnull(dnames) && error("r has no dimnames") dsym = rcopy(Array{Symbol}, getnames(dnames)) - for i in 1:length(dsym) + for i in eachindex(dsym) if dsym[i] == Symbol("") dsym[i] = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symbol(:dim_, i) end diff --git a/src/convert/base.jl b/src/convert/base.jl index 2463cd4f..ac07c907 100644 --- a/src/convert/base.jl +++ b/src/convert/base.jl @@ -114,7 +114,7 @@ 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 @@ -122,7 +122,7 @@ 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,7 +135,7 @@ 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 @@ -143,7 +143,7 @@ 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,8 @@ 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] + for (i, idx) in enumerate(eachindex(a)) + ra[i] = a[idx] end finally unprotect(1) @@ -296,8 +296,8 @@ 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] + for (i, idx) in enumerate(eachindex(a)) + ra[i] = a[idx] end finally unprotect(1) From a7c44c3d8b38a7cc1205fb6710e4bb9ddcaea22d Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Sun, 21 Jul 2024 17:53:27 -0500 Subject: [PATCH 2/3] more eachindex --- src/convert/base.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/convert/base.jl b/src/convert/base.jl index ac07c907..b14375a3 100644 --- a/src/convert/base.jl +++ b/src/convert/base.jl @@ -143,7 +143,7 @@ end function rcopy(::Type{BitArray},s::Ptr{LglSxp}) a = BitArray(undef, size(s)...) v = unsafe_vec(s) - for i in eachindex(a,v) + for i in eachindex(a, v) a[i] = v[i] != 0 end a @@ -264,7 +264,9 @@ 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, idx) in enumerate(eachindex(a)) + # 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 @@ -296,7 +298,9 @@ end function sexp(::Type{RClass{:list}}, a::AbstractArray) ra = protect(allocArray(VecSxp, size(a)...)) try - for (i, idx) in enumerate(eachindex(a)) + # 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 From 31626b62e2bf0d4e823bde6e7a70ffc2af82e6eb Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 22 Jul 2024 21:03:54 -0500 Subject: [PATCH 3/3] eachindex method --- src/methods.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/methods.jl b/src/methods.jl index 2b5fbfab..6f3cbe17 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -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)) """ Set element of a VectorSxp by a label.