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 16 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
29 changes: 29 additions & 0 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,32 @@ 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

Modifies `dict` by transforming each value from `val` to `f(val)`.
Note that the type of `dict` cannot be changed: if `f(val)` is not an instance of the key type
of `dict` then it will be converted to the key type if possible and otherwise raise an error.

# Examples
```jldoctest
julia> d = Dict(:a => 1, :b => 2)
Dict{Symbol,Int64} with 2 entries:
:a => 1
:b => 2

julia> map!(v -> v-1, values(d))
Dict{Symbol,Int64} with 2 entries:
:a => 0
:b => 1
```
"""
function map!(f, iter::ValueIterator)
# 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
12 changes: 12 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,18 @@ end

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

function map!(f, iter::ValueIterator{<:Dict})
dict = iter.dict
vals = dict.vals
# @inbounds is here so the it gets propigated to isslotfiled
@inbounds for i = dict.idxfloor:lastindex(vals)
if isslotfilled(dict, i)
vals[i] = f(vals[i])
end
end
ndinsmore marked this conversation as resolved.
Show resolved Hide resolved
return vals
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
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

map!(f,iter::ValueIterator{<:WeakKeyDict})= map!(f, values(iter.dict.ht))
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
@testset "AbstractDict & Fallback" begin
mutable struct TestDict{K, V} <: AbstractDict{K, V}
dict::Dict{K, V}
function TestDict(args...)
d = Dict(args...)
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