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

safer indexing for AbstractArray #540

Merged
merged 4 commits into from
Jul 24, 2024
Merged

safer indexing for AbstractArray #540

merged 4 commits into from
Jul 24, 2024

Conversation

palday
Copy link
Collaborator

@palday palday commented Jul 19, 2024

closes # 431

apparently not on Mac

Copy link

codecov bot commented Jul 19, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 80.66%. Comparing base (b6f4f19) to head (31626b6).

Files Patch % Lines
src/convert/axisarray.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #540      +/-   ##
==========================================
+ Coverage   80.65%   80.66%   +0.01%     
==========================================
  Files          26       26              
  Lines        1701     1702       +1     
==========================================
+ Hits         1372     1373       +1     
  Misses        329      329              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

for i in 1:length(a)
ra[i] = a[i]
for (i, idx) in enumerate(eachindex(a))
ra[i] = a[idx]
Copy link
Member

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 since enumerate will start with 1.

Copy link
Collaborator Author

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 based

Copy link
Member

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 and v as 1-based arrays in the above rcopy methods which means the loop specifications there were safe prior to using eachindex. I assumed you were going for minimal assumptions but if not then that's fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

typeof(ra) is lacking a eachindex method and the fallback calls keys; I'm going to take a stab at defining something appopriate

@palday palday requested a review from ararslan July 19, 2024 22:36
@ararslan
Copy link
Member

MethodError: no method matching keys(::Ptr{StrSxp})

Looks like something is getting eachindexed that shouldn't be (or is missing a suitable definition, perhaps)

@palday palday enabled auto-merge (squash) July 23, 2024 19:08
@@ -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))
Copy link
Member

Choose a reason for hiding this comment

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

Feels weird to define eachindex for a Ptr but we also have iterate and getindex...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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...

# 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]
Copy link
Member

Choose a reason for hiding this comment

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

This feels like a situation where a copyto! method could make sense, assuming such a method doesn't already exist (I assume it doesn't)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

@palday palday merged commit 73e0a64 into master Jul 24, 2024
15 checks passed
@palday palday deleted the pa/eachindex branch July 24, 2024 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants