Skip to content

Commit

Permalink
ENH: Indexing notation for C-sets.
Browse files Browse the repository at this point in the history
Syntactic sugar for the `subpart` function.
  • Loading branch information
epatters committed Oct 20, 2020
1 parent 36fd6b6 commit bfe87ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,24 @@ end
""" Get subpart of part in C-set.
Both single and vectorized access are supported. Chaining, or composition, of
parts is also supported. For example, given a vertex-attributed graph `g`,
subparts is also supported. For example, given a vertex-attributed graph `g`,
```
subpart(g, e, [:src, :vattr])
```
returns the vertex attribute of the source vertex of the edge `e`.
returns the vertex attribute of the source vertex of the edge `e`. As a
shorthand, subparts can also be accessed by indexing:
```
g[e, :src] == subpart(g, e, :src)
```
Be warned that indexing with lists of subparts works as above:
`g[e,[:src,:vattr]]` is equivalent to `subpart(g, e, [:src,:vattr])`. This
differs from DataFrames but note that the alternative interpretation of
`[:src,:vattr]` as two independent columns does not even make sense, since they
have different domains (belong to different tables).
"""
subpart(acs::ACSet, part, name::Symbol) = subpart(acs,name)[part]
subpart(acs::ACSet, name::Symbol) = _subpart(acs,Val(name))
Expand All @@ -362,6 +373,8 @@ subpart_name(expr::GATExpr{:compose}) = mapreduce(subpart_name, vcat, args(expr)
end
end

Base.getindex(acs::ACSet, args...) = subpart(acs, args...)

""" Get superparts incident to part in C-set.
If the subpart is indexed, this takes constant time; otherwise, it takes linear
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 @@ -159,6 +159,11 @@ X, parent, height = TheoryDendrogram[[:X, :parent, :height]]
@test subpart(d, 3, id(X)) == 3
@test incident(d, 10, compose(parent, height)) == [1,2,3]

# Indexing syntax.
@test d[3, :parent] == 4
@test d[3, [:parent, :height]] == 10
@test d[:, :parent] == [4,4,4,5,5]

# Copying parts.
d2 = Dendrogram{Int}()
copy_parts!(d2, d, X=[4,5])
Expand Down

0 comments on commit bfe87ca

Please sign in to comment.