-
-
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
Add push! implementation for AbstractArray depending only on resize! #55470
Merged
oscardssmith
merged 1 commit into
JuliaLang:master
from
mkitti:mkitti-push-abstractarray
Aug 12, 2024
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3525,13 +3525,45 @@ julia> map(+, [1 2; 3 4], [1,10,100,1000], zeros(3,1)) # iterates until 3rd is | |
""" | ||
map(f, it, iters...) = collect(Generator(f, it, iters...)) | ||
|
||
# Generic versions of push! for AbstractVector | ||
# These are specialized further for Vector for faster resizing and setindexing | ||
function push!(a::AbstractVector{T}, item) where T | ||
# convert first so we don't grow the array if the assignment won't work | ||
itemT = item isa T ? item : convert(T, item)::T | ||
new_length = length(a) + 1 | ||
resize!(a, new_length) | ||
a[new_length] = itemT | ||
return a | ||
end | ||
|
||
# specialize and optimize the single argument case | ||
function push!(a::AbstractVector{Any}, @nospecialize x) | ||
new_length = length(a) + 1 | ||
resize!(a, new_length) | ||
a[new_length] = x | ||
return a | ||
end | ||
function push!(a::AbstractVector{Any}, @nospecialize x...) | ||
@_terminates_locally_meta | ||
na = length(a) | ||
nx = length(x) | ||
resize!(a, na + nx) | ||
for i = 1:nx | ||
a[na+i] = x[i] | ||
end | ||
return a | ||
end | ||
vtjnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# multi-item push!, pushfirst! (built on top of type-specific 1-item version) | ||
# (note: must not cause a dispatch loop when 1-item case is not defined) | ||
push!(A, a, b) = push!(push!(A, a), b) | ||
push!(A, a, b, c...) = push!(push!(A, a, b), c...) | ||
pushfirst!(A, a, b) = pushfirst!(pushfirst!(A, b), a) | ||
pushfirst!(A, a, b, c...) = pushfirst!(pushfirst!(A, c...), a, b) | ||
|
||
# sizehint! does not nothing by default | ||
sizehint!(a::AbstractVector, _) = 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. Unrelated change? Also no tests. 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. #51903 introduced a dependency on I will add tests for append. |
||
|
||
## hashing AbstractArray ## | ||
|
||
const hash_abstractarray_seed = UInt === UInt64 ? 0x7e2d6fb6448beb77 : 0xd4514ce5 | ||
|
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
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.
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.
I think this re-establishes the state before #51903 but I agree this is an issue.
Would
end
work better here or should we require or dispatch on one-based indexing?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.
on the specific case of
OffsetArrays
,resize!
works on the the parent array (solength(a) + 1
is 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.
I'm not sure I understand your point.