Skip to content

Commit

Permalink
Merge pull request #39 from AlgebraicJulia/copy_parts_attrvar
Browse files Browse the repository at this point in the history
Reindex attribute variables in `copy_parts!`
  • Loading branch information
epatters authored Jun 26, 2023
2 parents 54543a9 + 187a792 commit 8c8800e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/DenseACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,16 @@ ACSetInterface.copy_parts_only!(to::ACSet, from::ACSet, parts::NamedTuple) =
),))

@ct_ctrl for (a,d,c) in relevant_attrs
set_subpart!(to, newparts[@ct d], @ct(a), subpart(from, parts[@ct d], @ct(a)))
for (part, val) in zip(newparts[@ct d], subpart(from, parts[@ct d], @ct(a)))
if !(val isa AttrVar)
set_subpart!(to, part, @ct(a), val)
else
newindex = findfirst(==(val.val), get(parts, @ct(c), []))
if !isnothing(newindex)
set_subpart!(to, part, @ct(a), AttrVar(newparts[@ct c][newindex]))
end
end
end
end

newparts
Expand Down
13 changes: 7 additions & 6 deletions test/ACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ dgram_makers = [
for (dgram_maker, ldgram_maker) in dgram_makers
d = dgram_maker(Int)
add_parts!(d, :X, 3, height=0)
add_parts!(d, :X, 2, height=[10,20])
add_parts!(d, :R, 2)
add_parts!(d, :X, 2, height=[10,AttrVar(add_part!(d,:R))])
set_subpart!(d, 1:3, :parent, 4)
set_subpart!(d, [4,5], :parent, 5)

Expand All @@ -201,7 +202,7 @@ for (dgram_maker, ldgram_maker) in dgram_makers
@test has_subpart(d, :height)
@test subpart(d, [1,2,3], :height) == [0,0,0]
@test subpart(d, 4, :height) == 10
@test subpart(d, :, :height) == [0,0,0,10,20]
@test subpart(d, :, :height) == [0,0,0,10,AttrVar(3)]

# Chained accessors.
@test subpart(d, 3, [:parent, :parent]) == 5
Expand All @@ -212,7 +213,7 @@ for (dgram_maker, ldgram_maker) in dgram_makers
# Indexing syntax.
@test d[3, :parent] == 4
@test d[3, [:parent, :height]] == 10
@test d[3:5, [:parent, :height]] == [10,20,20]
@test d[3:5, [:parent, :height]] == [10,AttrVar(3),AttrVar(3)]
@test d[:, :parent] == [4,4,4,5,5]
d2 = copy(d)
d2[1, :parent] = 1
Expand All @@ -223,15 +224,15 @@ for (dgram_maker, ldgram_maker) in dgram_makers

# Copying parts.
d2 = dgram_maker(Int)
copy_parts!(d2, d, X=[4,5])
copy_parts!(d2, d, X=[4,5], R=[3])
@test nparts(d2, :X) == 2
@test subpart(d2, [1,2], :parent) == [2,2]
@test subpart(d2, [1,2], :height) == [10,20]
@test subpart(d2, [1,2], :height) == [10, AttrVar(1)]

du = disjoint_union(d, d2)
@test nparts(du, :X) == 7
@test subpart(du, :parent) == [4,4,4,5,5,7,7]
@test subpart(du, :height) == [0,0,0,10,20,10,20]
@test subpart(du, :height) == [0,0,0,10,AttrVar(3),10,AttrVar(4)]

# Pretty printing of data attributes.
s = sprint(show, d)
Expand Down

0 comments on commit 8c8800e

Please sign in to comment.