Skip to content

Commit 3a2aedb

Browse files
authored
Merge pull request #371 from probcomp/alexlew-has-submap
Add `has_submap` method for ChoiceMaps (#124)
2 parents e0e31b1 + b926040 commit 3a2aedb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/choice_map.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ no value exists. A syntactic sugar is `Base.getindex`:
3030
"""
3131
function get_value end
3232

33+
"""
34+
has_submap(choices::ChoiceMap, addr)
35+
36+
Return true if there is a non-empty sub-assignment at the given address.
37+
"""
38+
function has_submap end
39+
3340
"""
3441
key_submap_iterable = get_submaps_shallow(choices::ChoiceMap)
3542
@@ -69,6 +76,8 @@ function Base.isempty(::ChoiceMap)
6976
true
7077
end
7178

79+
80+
@inline has_submap(choices::ChoiceMap, addr) = !has_value(choices, addr) && !isempty(get_submap(choices, addr))
7281
@inline get_submap(choices::ChoiceMap, addr) = EmptyChoiceMap()
7382
@inline has_value(choices::ChoiceMap, addr) = false
7483
@inline get_value(choices::ChoiceMap, addr) = throw(KeyError(addr))
@@ -323,6 +332,7 @@ end
323332

324333
export ChoiceMap
325334
export get_address_schema
335+
export has_submap
326336
export get_submap
327337
export get_value
328338
export has_value

test/assignment.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,29 @@ end
267267
# overwrite value with a submap
268268
choices = choicemap()
269269
choices[:x] = 1
270+
@test has_value(choices, :x)
271+
@test !has_submap(choices, :x)
270272
submap = choicemap(); submap[:y] = 2
271273
set_submap!(choices, :x, submap)
272274
@test !has_value(choices, :x)
273275
@test !isempty(get_submap(choices, :x))
276+
@test has_submap(choices, :x)
274277

275278
# overwrite subassignment with a value
276279
choices = choicemap()
277280
choices[:x => :y] = 1
278281
choices[:x] = 2
279282
threw = false
283+
@test !has_submap(choices, :x)
280284
try get_submap(choices, :x) catch KeyError threw = true end
281285
@test threw
282286
@test choices[:x] == 2
283287

284288
# overwrite subassignment with a subassignment
285289
choices = choicemap()
286290
choices[:x => :y] = 1
291+
@test has_submap(choices, :x)
292+
@test !has_submap(choices, :x => :y)
287293
submap = choicemap(); submap[:z] = 2
288294
set_submap!(choices, :x, submap)
289295
@test !isempty(get_submap(choices, :x))

0 commit comments

Comments
 (0)