Skip to content

Commit eea0e0c

Browse files
committed
Added implementation of getnull(coll, key) for Associative.
This returns a Nullable value corresponding to the key (or null). Includes specialization for dictionaries. Fixes #13055
1 parent 7199111 commit eea0e0c

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

base/dict.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
66

77
haskey(d::Associative, k) = in(k,keys(d))
88

9+
"""
10+
getnull(d::Associative, key)
11+
12+
Return Nullable(`value`) corresponding to `key` if present, otherwise null.
13+
"""
14+
getnull{K,V}(d::Associative{K,V}, k) = haskey(d, k) ? Nullable{V}(d[k]::V) : Nullable{V}()
15+
916
function in(p::Pair, a::Associative, valcmp=(==))
1017
v = get(a,p[1],secret_table_token)
1118
if !is(v, secret_table_token)
@@ -698,6 +705,11 @@ function get{K,V}(default::Callable, h::Dict{K,V}, key)
698705
return (index < 0) ? default() : h.vals[index]::V
699706
end
700707

708+
function getnull{K,V}(h::Dict{K,V}, key)
709+
index = ht_keyindex(h, key)
710+
return (index<0) ? Nullable{V}() : Nullable{V}(h.vals[index]::V)
711+
end
712+
701713
haskey(h::Dict, key) = (ht_keyindex(h, key) >= 0)
702714
in{T<:Dict}(key, v::KeyIterator{T}) = (ht_keyindex(v.dict, key) >= 0)
703715

@@ -828,6 +840,7 @@ function getindex(dict::ImmutableDict, key)
828840
end
829841
throw(KeyError(key))
830842
end
843+
831844
function get(dict::ImmutableDict, key, default)
832845
while isdefined(dict, :parent)
833846
dict.key == key && return dict.value
@@ -836,6 +849,14 @@ function get(dict::ImmutableDict, key, default)
836849
return default
837850
end
838851

852+
function getnull{K,V}(dict::ImmutableDict{K,V}, key)
853+
while isdefined(dict, :parent)
854+
dict.key == key && return Nullable{V}(dict.value::V)
855+
dict = dict.parent
856+
end
857+
Nullable{V}()
858+
end
859+
839860
# this actually defines reverse iteration (e.g. it should not be used for merge/copy/filter type operations)
840861
start(t::ImmutableDict) = t
841862
next{K,V}(::ImmutableDict{K,V}, t) = (Pair{K,V}(t.key, t.value), t.parent)

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ export
749749
get!,
750750
getindex,
751751
getkey,
752+
getnull,
752753
haskey,
753754
in,
754755
intersect!,

base/weakkeydict.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ get{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> get(wkh.ht, key, default)
4242
get{K}(default::Callable, wkh::WeakKeyDict{K}, key) = lock(() -> get(default, wkh.ht, key), wkh)
4343
get!{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> get!(wkh.ht, key, default), wkh)
4444
get!{K}(default::Callable, wkh::WeakKeyDict{K}, key) = lock(() -> get!(default, wkh.ht, key), wkh)
45+
getnull{K}(wkh::WeakKeyDict{K}, key) = lock(() -> getnull(wkh.ht, key), wkh)
4546
pop!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> pop!(wkh.ht, key), wkh)
4647
pop!{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> pop!(wkh.ht, key, default), wkh)
4748
delete!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> delete!(wkh.ht, key), wkh)

test/dict.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ let f(x) = x^2, d = Dict(8=>19)
256256
f(4)
257257
end == 16
258258

259+
# getnull (returns Nullable)
260+
@test get(getnull(d, 8)) == 19
261+
@test isnull(getnull(d, 13))
262+
259263
@test d == Dict(8=>19, 19=>2, 42=>4)
260264
end
261265

@@ -452,6 +456,10 @@ let d = ImmutableDict{String, String}(),
452456
@test get(d4, "key1", :default) === v2
453457
@test get(d4, "foo", :default) === :default
454458
@test get(d, k1, :default) === :default
459+
@test getnull(d1, "key1") === Nullable{String}(v1)
460+
@test getnull(d4, "key1") === Nullable{String}(v2)
461+
@test getnull(d4, "foo") === Nullable{String}()
462+
@test getnull(d, k1) === Nullable{String}()
455463
@test d1["key1"] === v1
456464
@test d4["key1"] === v2
457465
@test similar(d3) === d

0 commit comments

Comments
 (0)