Skip to content

Commit

Permalink
Merge pull request #464 from epatters/index-tuple-attributes
Browse files Browse the repository at this point in the history
Don't vectorize `incident` when attribute is a tuple
  • Loading branch information
epatters authored Jul 13, 2021
2 parents ac03b82 + b6f7abc commit caece4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,23 +436,28 @@ incident(acs::ACSet, part, expr::GATExpr; kw...) =
copy ? Base.copy.(indices) : indices
end
else
:(broadcast_findall(part, acs.tables.$(dom(CD,name)).$name))
:(vectorized_findall(part, acs.tables.$(dom(CD,name)).$name))
end
elseif name attr(AD)
if name Idxed
quote
indices = get_data_index.(Ref(acs.indices.$name), part)
indices = vectorized_data_index(acs.indices.$name, part)
copy ? Base.copy.(indices) : indices
end
else
:(broadcast_findall(part, acs.tables.$(dom(AD,name)).$name))
:(vectorized_findall(part, acs.tables.$(dom(AD,name)).$name))
end
else
throw(ArgumentError("$(repr(name)) not in $(hom(CD)) or $(attr(AD))"))
end
end

broadcast_findall(xs, array::AbstractArray) =
# FIXME: This is not a reliable way to decide whether to vectorize. What if the
# attribute type is itself an array?
vectorized_data_index(d, x) = get_data_index(d, x)
vectorized_data_index(d, xs::AbstractArray) = get_data_index.(Ref(d), xs)
vectorized_findall(x, array::AbstractArray) = findall(y -> x == y, array)
vectorized_findall(xs::AbstractArray, array::AbstractArray) =
broadcast(x -> findall(y -> x == y, array), xs)

""" Add part of given type to C-set, optionally setting its subparts.
Expand Down
5 changes: 5 additions & 0 deletions test/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ set_subpart!(lset, 1, :label, :baz)
set_subpart!(lset, 3, :label, :biz)
@test incident(lset, :foo, :label) == []

# Labeled set with compound label (tuple).
lset = IndexedLabeledSet{Tuple{Int,Int}}()
add_parts!(lset, :X, 2, label=[(1,1), (1,2)])
@test incident(lset, (1,2), :label) == [2]

# Deletion with indexed data attribute.
lset = IndexedLabeledSet{Symbol}()
add_parts!(lset, :X, 3, label=[:foo, :foo, :bar])
Expand Down

0 comments on commit caece4c

Please sign in to comment.