-
Notifications
You must be signed in to change notification settings - Fork 244
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: Added DefaultDict #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Dictionary which returns (and sets) a default value for a requested item not | ||
# already in to the dictionary | ||
|
||
immutable DefaultDict{K,V,F,D<:Associative} <: Associative{K,V} | ||
default::F | ||
d::D | ||
|
||
DefaultDict(x::F, kv::AbstractArray{(K,V)}) = new(x, D{K,V}(kv)) | ||
DefaultDict(x::F, d::DefaultDict) = DefaultDict(x, d.d) | ||
DefaultDict(x::F, d::D=D{K,V}()) = new(x, d) | ||
DefaultDict(x, ks, vs) = new(x, D{K,V}(ks,vs)) | ||
end | ||
|
||
DefaultDict() = error("DefaultDict: no default specified") | ||
DefaultDict(k,v) = error("DefaultDict: no default specified") | ||
|
||
# TODO: these mimic similar Dict constructors, but may not be needed | ||
DefaultDict{K,V,F}(default::F, ks::AbstractArray{K}, vs::AbstractArray{V}) = DefaultDict{K,V,F,Dict}(default,ks,vs) | ||
DefaultDict{F}(default::F,ks,vs) = DefaultDict{Any,Any,F,Dict}(default, ks, vs) | ||
|
||
# syntax entry points | ||
DefaultDict{F}(default::F) = DefaultDict{Any,Any,F,Dict}(default) | ||
DefaultDict{K,V,F}(::Type{K}, ::Type{V}, default::F) = DefaultDict{K,V,F,Dict}(default) | ||
DefaultDict{K,V,F}(default::F, kv::AbstractArray{(K,V)}) = DefaultDict{K,V,F,Dict}(default, kv) | ||
DefaultDict{F,D<:Associative}(default::F, d::D) = ((K,V)=eltype(d); DefaultDict{K,V,F,D}(default, d)) | ||
|
||
similar{K,V,F,D}(d::DefaultDict{K,V,F,D}) = DefaultDict{K,V,F,D}() | ||
|
||
sizehint(d::DefaultDict) = sizehint(d.d) | ||
empty!(d::DefaultDict) = empty!(d.d) | ||
setindex!(d::DefaultDict, v, k) = setindex!(d.d, v, k) | ||
|
||
# Note that getindex depends on the particular implementation of Dict in Base. | ||
# If the Dict implementation changes, this may break. | ||
# Also note that we hash twice here if the key is not in the dictionary: once | ||
# when retrieving, and once when assigning. | ||
function getindex{K,V,F<:Base.Callable}(d::DefaultDict{K,V,F}, key) | ||
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. Why not simply use 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. As of right now, I'm mimicking Python here. I think that choosing whether or not 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. Actually, unless we don't modify the dictionary at all, simply calling Abstractly, what would be useful is a function which, e.g, returns a |
||
index = Base.ht_keyindex(d.d, key) | ||
if index < 0 | ||
d.d[key] = ret = convert(V, d.default()) | ||
return ret::V | ||
end | ||
return d.d.vals[index]::V | ||
end | ||
|
||
function getindex{K,V}(d::DefaultDict{K,V}, key) | ||
index = Base.ht_keyindex(d.d, key) | ||
if index < 0 | ||
d.d[key] = ret = convert(V, d.default) | ||
return ret::V | ||
end | ||
return d.d.vals[index]::V | ||
end | ||
|
||
get(d::DefaultDict, key, deflt) = get(d.d, key, deflt) | ||
|
||
haskey(d::DefaultDict, key) = haskey(d.d, key) | ||
in{T<:DefaultDict}(key, v::Base.KeyIterator{T}) = key in keys(v.dict.d) | ||
getkey(d::DefaultDict, key, deflt) = getkey(d.d, key, deflt) | ||
|
||
pop!(d::DefaultDict, key) = pop!(d.d, key) | ||
delete!(d::DefaultDict, key) = delete!(d.d, key) | ||
|
||
start(d::DefaultDict) = start(d.d) | ||
done(d::DefaultDict, i) = done(d.d,i) | ||
next(d::DefaultDict, i) = next(d.d,i) | ||
|
||
isempty(d::DefaultDict) = isempty(d.d) | ||
length(d::DefaultDict) = length(d.d) | ||
|
||
next{T<:DefaultDict}(v::Base.KeyIterator{T}, i) = (v.dict.d.keys[i], Base.skip_deleted(v.dict.d,i+1)) | ||
next{T<:DefaultDict}(v::Base.ValueIterator{T}, i) = (v.dict.d.vals[i], Base.skip_deleted(v.dict.d,i+1)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using DataStructures | ||
using Base.Test | ||
|
||
# construction | ||
@test_throws DefaultDict() | ||
@test_throws DefaultDict(String, Int) | ||
|
||
# empty dictionary | ||
d = DefaultDict(Char, Int, 1) | ||
@test length(d) == 0 | ||
@test isempty(d) | ||
@test d['c'] == 1 | ||
@test !isempty(d) | ||
empty!(d) | ||
@test isempty(d) | ||
|
||
# access, modification | ||
@test (d['a'] += 1) == 2 | ||
@test 'a' in keys(d) | ||
@test haskey(d, 'a') | ||
@test get(d, 'b', 0) == 0 | ||
@test !('b' in keys(d)) | ||
@test !haskey(d, 'b') | ||
@test pop!(d, 'a') == 2 | ||
@test isempty(d) | ||
|
||
for c in 'a':'z' | ||
d[c] = c-'a'+1 | ||
end | ||
|
||
@test d['z'] == 26 | ||
@test d['@'] == 1 | ||
@test length(d) == 27 | ||
delete!(d, '@') | ||
@test length(d) == 26 | ||
|
||
for (k,v) in d | ||
@test v == k-'a'+1 | ||
end | ||
|
||
@test sort(collect(keys(d))) == ['a':'z'] | ||
@test sort(collect(values(d))) == [1:26] | ||
|
||
# Starting from an existing dictionary | ||
e = ['a'=>1, 'b'=>3, 'c'=>5] | ||
f = DefaultDict(0, e) | ||
@test_throws e['d'] | ||
@test f['d'] == 0 | ||
f['e'] = 9 | ||
@test e['d'] == 0 | ||
@test e['e'] == 9 |
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.
Why not
default::V
, and use aconvert(V, x)
in the constructor?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.
As noted in the docs,
default
could either be a value or aCallable
(hence, the 2 versions ofgetindex
below). I do useconvert(V,x)
in both versions ofgetindex
. If it happens thatV
andF
are the same value, that should be a no-op, although I don't know how smart the compiler is.Aside: this is actually a divergence from python, which only allows defaults from functions, but multiple dispatch makes this easy in Julia.