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

How to efficiently modify all values of a SortedDict? #812

Open
dpinol opened this issue May 19, 2022 · 2 comments
Open

How to efficiently modify all values of a SortedDict? #812

dpinol opened this issue May 19, 2022 · 2 comments

Comments

@dpinol
Copy link
Contributor

dpinol commented May 19, 2022

With normal Dict I can

b=Dict(3=>4)
map!(v->v*2, values(b))

b
Dict{Int64, Int64} with 1 entry:
  3 => 8

With SortedDict I can't

a=SortedDict(2=>3)
map!(v->v*2, values(a))

ERROR: MethodError: no method matching map!(::var"#7#8", ::DataStructures.SDMValIteration{SortedDict{Int64, Int64, Base.Order.ForwardOrdering}})
Closest candidates are:
  map!(::Any, ::Base.ValueIterator{<:Dict}) at dict.jl:736
  map!(::Any, ::Base.ValueIterator{<:WeakKeyDict}) at weakkeydict.jl:140
  map!(::Tf, ::SparseArrays.AbstractSparseMatrixCSC, ::Union{LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.LowerTriangular, LinearAlgebra.SymTridiagonal, LinearAlgebra.Tridiagonal, LinearAlgebra.UnitLowerTriangular, LinearAlgebra.UnitUpperTriangular, LinearAlgebra.UpperTriangular, SparseArrays.SparseMatrixCSC}, ::Union{LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.LowerTriangular, LinearAlgebra.SymTridiagonal, LinearAlgebra.Tridiagonal, LinearAlgebra.UnitLowerTriangular, LinearAlgebra.UnitUpperTriangular, LinearAlgebra.UpperTriangular, SparseArrays.SparseMatrixCSC}...) where {Tf, N} at /opt/julia/julia-1.8.0-beta3/share/julia/stdlib/v1.8/SparseArrays/src/higherorderfns.jl:1162

I can do

for (k,v) in a
       a[k]*=2
       end

but it would be less efficient

thanks

@StephenVavasis
Copy link
Contributor

This is not implemented. I am working on the sorted containers currently (#787) but this functionality is not part of the current project. As a temporary response to this issue, I have updated the docs of that PR to explicitly mention the missing functionality. Meanwhile, the example below shows how to obtain this functionality efficiently using a loop. The point of this loop, compared to the loop in the original posting, is that semitokens can be dereferenced in O(1) operations whereas keys require O(log n) operations.

julia> s = SortedDict(3=>4)
SortedDict{Int64, Int64, Base.Order.ForwardOrdering} with 1 entry:
  3 => 4

julia> for t in onlysemitokens(s)
           s[t] *= 2
       end

julia> s
SortedDict{Int64, Int64, Base.Order.ForwardOrdering} with 1 entry:
  3 => 8

@dpinol
Copy link
Contributor Author

dpinol commented May 24, 2022

@StephenVavasis thank you for the tip. It works great 👍

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

No branches or pull requests

2 participants