Skip to content

Commit

Permalink
ENH: Get generators from presentations by indexing.
Browse files Browse the repository at this point in the history
Closes #301.
  • Loading branch information
epatters committed Oct 20, 2020
1 parent ee276bc commit 1da85b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/core/Present.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ generators(pres::Presentation, type::Symbol) = pres.generators[type]
generators(pres::Presentation, type::Type) = generators(pres, nameof(type))

""" Retrieve generators by name.
Generators can also be retrieved using indexing notation, so that
`generator(pres, name)` and `pres[name]` are equivalent.
"""
function generator(pres::Presentation, name)
type, index = pres.generator_name_index[name]
pres.generators[type][index]
end
Base.getindex(pres::Presentation, name) = generator.(Ref(pres), name)

""" Does the presentation contain a generator with the given name?
"""
Expand Down
2 changes: 1 addition & 1 deletion test/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ set_subpart!(d, [4,5], :parent, 5)
@test incident(d, 5, [:parent, :parent]) == [1,2,3,4,5]
@test incident(d, 10, [:parent, :height]) == [1,2,3]

X, parent, height = generator.(Ref(TheoryDendrogram), [:X, :parent, :height])
X, parent, height = TheoryDendrogram[[:X, :parent, :height]]
@test subpart(d, 3, parent) == 4
@test subpart(d, 3, compose(parent, height)) == 10
@test subpart(d, 3, id(X)) == 3
Expand Down
16 changes: 9 additions & 7 deletions test/core/Present.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ pres = Presentation(FreeCategory)
@test presentation_theory(pres) == Category
@test !has_generator(pres, :A)
add_generator!(pres, A)
@test generators(pres) == [ A ]
@test generators(pres) == [A]
@test generator(pres, :A) == A
@test has_generator(pres, :A)
add_generator!(pres, B)
@test generators(pres) == [ A, B ]
@test generators(pres) == [A, B]
@test_throws Exception add_generator!(pres, A)
@test pres[:A] == A
@test pres[[:A,:B]] == [A, B]

add_generators!(pres, (f,g))
@test generators(pres) == [ A, B, f, g ]
@test generators(pres, :Ob) == [ A, B ]
@test generators(pres, :Hom) == [ f, g ]
@test generators(pres, FreeCategory.Ob) == [ A, B ]
@test generators(pres, FreeCategory.Hom) == [ f, g ]
@test generators(pres) == [A, B, f, g]
@test generators(pres, :Ob) == [A, B]
@test generators(pres, :Hom) == [f, g]
@test generators(pres, FreeCategory.Ob) == [A, B]
@test generators(pres, FreeCategory.Hom) == [f, g]

# Presentation macro
####################
Expand Down

0 comments on commit 1da85b8

Please sign in to comment.