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

Copy both indices and values #101

Closed
wants to merge 2 commits into from
Closed

Conversation

mtfishman
Copy link
Contributor

Fixes #98.

@mtfishman mtfishman mentioned this pull request Apr 29, 2022
@codecov
Copy link

codecov bot commented Apr 29, 2022

Codecov Report

Merging #101 (6209544) into master (1510dac) will increase coverage by 0.40%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #101      +/-   ##
==========================================
+ Coverage   77.08%   77.48%   +0.40%     
==========================================
  Files          18       18              
  Lines        1999     1999              
==========================================
+ Hits         1541     1549       +8     
+ Misses        458      450       -8     
Impacted Files Coverage Δ
src/AbstractDictionary.jl 87.14% <100.00%> (+1.42%) ⬆️
src/ArrayDictionary.jl 84.84% <100.00%> (ø)
src/Dictionary.jl 79.89% <100.00%> (ø)
src/broadcast.jl 57.53% <0.00%> (+5.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1510dac...6209544. Read the comment docs.

@mtfishman
Copy link
Contributor Author

@andyferris my main concern here is defining the generic version of copy as:

function Base.copy(d::AbstractDictionary, ::Type{T}) where {T}
    out = similar(copy(keys(d)), T)
    copyto!(out, d)
    return out
end

instead of:

function Base.copy(d::AbstractDictionary, ::Type{T}) where {T}
    out = similar(d, T)
    copyto!(out, d)
    return out
end

It seems like this assumes a one-to-one mapping between AbstractIndices types and AbstractDictionary types, so if another AbstractDictionary was defined with Indices it would switch the type to Dictionary, for example.

Would it make sense to make similar(d::AbstractDictionary, T) copy the indices as well? In that way the original version of copy would work (similar seems like the right function for that to preserve the original type), and it probably makes sense that similar is also safe from changing the indices.

@andyferris
Copy link
Owner

Yeah that's one reason why it was implemented how it is, because this is a bit awkward. As it stands you'd almost not want an abstract definition, and instead force every dictionary implementation to define copy. I don't really like that.

I think the proper fix here is to finish and merge #54 and then define it like:

function Base.copy(d::AbstractDictionary, ::Type{T}) where {T}
    out = similar_type(d, T)(copy(keys(d)))
    copyto!(out, d)
    return out
end

Copy link
Owner

@andyferris andyferris left a comment

Choose a reason for hiding this comment

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

Awesome. I think this will be a nice improvement. We might need to tag 0.4 though (and see if there are other breaking changes to do).

Do you think it is worth doing #54 at the same time to make the abstract method more robust? Or merge this first an worry about #54 later?

@aplavin
Copy link
Contributor

aplavin commented Mar 2, 2023

@andyferris would be nice to have it! Would you merge & release please?

@mtfishman
Copy link
Contributor Author

Sorry, this one slipped my mind and now I forget the status. Maybe worth just merging this without doing #54? That seems like a bigger issue (though very important, we are designing a similar_type function in ITensors.jl for making operations more generic across tensor types).

@aplavin
Copy link
Contributor

aplavin commented Jan 29, 2024

Another yearly bump @andyferris ...

Copy link
Owner

@andyferris andyferris left a comment

Choose a reason for hiding this comment

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

Thanks for the reminder. Sometimes I sway either way about whether this should happen, or not, but I think users will appreciate this. (That said, I don't get much feedback on Dictionaries.jl beyond the occassional "it works").

Since this PR has been written, we have added UnorderedDictionary which now also need a copy method before we can merge.

"""
copy(dict::AbstractDictionary)
copy(dict::AbstractDictionary, ::Type{T})

Create a shallow copy of the values of `dict`. Note that `keys(dict)` is not copied, and
therefore care must be taken that inserting/deleting elements. A new element type `T` can
Create a shallow copy of the values of `dict`. A new element type `T` can
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we should say "values and indices of dict"?

@andyferris andyferris mentioned this pull request Jan 30, 2024
@andyferris
Copy link
Owner

Due to confilcts I've moved this to #136.

@andyferris
Copy link
Owner

Thanks again @mtfishman and @aplavin

@andyferris andyferris closed this Jan 30, 2024
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.

set! is not copy safe
3 participants