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

Clear parts before assigning in case of injective cache #59

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/ACSetInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using Tables
using PrettyTables: pretty_table

using ..ColumnImplementations: AttrVar
using ..PreimageCaches: is_injective

using ..Schemas: types, attrs, attrtypes

Expand Down Expand Up @@ -250,6 +251,9 @@ function set_subpart! end
# Inlined for the same reason as `subpart`.

@inline function set_subpart!(acs::ACSet , parts::Union{AbstractVector{Int}, AbstractSet{Int}}, name, vals)
if is_injective(acs.subparts[name])
clear_subpart!(acs, parts, name)
end
broadcast(parts, vals) do part, val
set_subpart!(acs, part, name, val)
end
Expand Down
4 changes: 3 additions & 1 deletion src/Columns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using Reexport
@reexport using ..PreimageCaches

import ..Mappings: view_with_default
import ..PreimageCaches: preimage, preimage_multi
import ..PreimageCaches: preimage, preimage_multi, is_injective

# Columns
#########
Expand Down Expand Up @@ -94,4 +94,6 @@ view_with_default(c::Column, xs, def) = ColumnView(c, xs, def)

Base.size(c::ColumnView) = size(c.indices)

is_injective(c::Column) = is_injective(c.pc)

end
11 changes: 11 additions & 0 deletions src/PreimageCaches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ performance implications.
- [`preimage`](@ref)
- [`assign!`](@ref)
- [`unassign!`](@ref)
- [`is_injective`](@ref) (defaults to false if not implemented)
"""
abstract type PreimageCache{S,T} end

Expand Down Expand Up @@ -94,6 +95,14 @@ Remove `x` from the preimage of `y`.
"""
function unassign! end

"""
Whether or not the cache imposes an injectivity constraint. (default: false)
"""
is_injective(::PreimageCache)::Bool = false

# Caches
########

"""
The trivial preimage mapping. It just computes preimages on the fly, and the
operations for updating it are noops
Expand Down Expand Up @@ -203,4 +212,6 @@ function unassign!(pc::InjectiveCache{S,T}, y::T, x::S) where {S,T}
delete!(pc.inverse, y)
end

is_injective(::InjectiveCache)::Bool = true

end
2 changes: 2 additions & 0 deletions test/ACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ for lset_maker in lset_makers
@test isempty(incident(lset, :foo, :label))

@test_throws Exception set_subpart!(lset, 1, :label, :bar)
set_subpart!(lset, :, :label, [:bar, :foo])
@test subpart(lset, :, :label) == [:bar, :foo]
end

SchDecGraph = BasicSchema([:E,:V], [(:src,:E,:V),(:tgt,:E,:V)],
Expand Down