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

RFC/WIP: creation of mapdict method to modify Dict values #31223

Merged
merged 18 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,29 @@ function iterate(s::IdSet, state...)
((k, _), i) = y
return (k, i)
end

"""
map!(f, values(dict::AbstractDict))
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
Takes the function `f(value) and transforms values stored in `dict` with the transformation `value=f(value).
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved

# Examples
```jldoctest
julia> D=Dict(:a=>1,:b=>2)
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
Dict{Symbol,Int64} with 2 entries:
:a => 1
:b => 2

julia> map!(v->v-1,values(D))
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
Dict{Symbol,Int64} with 2 entries:
:a => 0
:b => 1
```
"""
function map!(f, iter::Base.ValueIterator{D}) where D <:Base.AbstractDict
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
# This is the naive fallback which requires hash evaluations
# Contrary to the example Dict has an implementation which does not require hash evaluations
dict = iter.dict
for (key, val) in pairs(dict)
dict[key] = f(val)
end
end
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,20 @@ end

filter!(f, d::Dict) = filter_in_one_pass!(f, d)

function map!(f, iter::ValueIterator{Dict{K,V}}) where {K, V}
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
dict = iter.dict
vals = dict.vals
i = dict.idxfloor
len=length(vals)
@inbounds while i < len
if !(isslotfilled(dict, i))
i += 1; continue
end
@inbounds vals[i] = f(vals[i])
i += 1; continue
end
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
end

struct ImmutableDict{K,V} <: AbstractDict{K,V}
parent::ImmutableDict{K,V}
key::K
Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ export
length,
map!,
map,
mapdict!,
mapdict,
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
mapfoldl,
mapfoldr,
mapreduce,
Expand Down
1 change: 1 addition & 0 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function getkey(wkh::WeakKeyDict{K}, kk, default) where K
end
end

Base.map!(f,iter::Base.ValueIterator{WeakKeyDict{K,V}}) where {K, V} = Base.map!(f,values(iter.dict.ht))
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
get(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get(wkh.ht, key, default), wkh)
get(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get(default, wkh.ht, key), wkh)
function get!(wkh::WeakKeyDict{K}, key, default) where {K}
Expand Down
25 changes: 25 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,28 @@ end
end
end
end

@testset "map!(f,values(dict))" begin
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
@testset "AbstractDict & Fallback" begin
mutable struct TestDict{K, V} <: AbstractDict{K, V}
dict::Dict{K, V}
function TestDict(args...)
d=Dict(args...)
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
new{keytype(d), valtype(d)}(d)
end
end
Base.setindex!(td::TestDict, args...) = setindex!(td.dict, args...)
Base.getindex(td::TestDict, args...) = getindex(td.dict, args...)
Base.pairs(D::TestDict) = pairs(D.dict)
testdict = TestDict(:a=>1, :b=>2)
map!(v->v-1, values(testdict))
@test testdict[:a] == 0
@test testdict[:b] == 1
end
@testset "Dict" begin
testdict = Dict(:a=>1, :b=>2)
map!(v->v-1, values(testdict))
@test testdict[:a] == 0
@test testdict[:b] == 1
end
end