Skip to content

Commit

Permalink
Enable ordinal indexing of DataGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 26, 2024
1 parent 94ebb02 commit 3d2cb9c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@ for f in [:(NamedGraphs.position_graph), :(NamedGraphs.vertex_positions)]
end
end
end

using Graphs: edgetype, vertices
using NamedGraphs.OrdinalIndexing: OrdinalSuffixedInteger
# TODO: Define through some intermediate `to_vertex` function
# (analagous to Julia's `to_indices`) instead of through
# overloading `Base.getindex`.
function Base.getindex(graph::AbstractDataGraph, vertex::OrdinalSuffixedInteger)
return graph[vertices(graph)[vertex]]
end
function Base.getindex(
graph::AbstractDataGraph, edge::Pair{<:OrdinalSuffixedInteger,<:OrdinalSuffixedInteger}
)
return graph[edgetype(graph)(vertices(graph)[edge[1]], vertices(graph)[edge[2]])]
end
function Base.setindex!(graph::AbstractDataGraph, value, vertex::OrdinalSuffixedInteger)
graph[vertices(graph)[vertex]] = value
return graph
end
function Base.setindex!(
graph::AbstractDataGraph,
value,
edge::Pair{<:OrdinalSuffixedInteger,<:OrdinalSuffixedInteger},
)
graph[edgetype(graph)(vertices(graph)[edge[1]], vertices(graph)[edge[2]])] = value
return graph
end
end
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using GraphsFlows: GraphsFlows
using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph
using NamedGraphs.GraphsExtensions: , rename_vertices, vertextype
using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph
using NamedGraphs.OrdinalIndexing: nd, st, rd, th
using Test: @test, @test_broken, @testset

using DataGraphs: is_arranged
Expand Down Expand Up @@ -398,5 +399,34 @@ using DataGraphs: is_arranged
@test verts[4] part2
@test flow == 1
end
@testset "OrdinalIndexing" begin
g = DataGraph(
NamedGraph(path_graph(3), ["a", "b", "c"]);
vertex_data_eltype=String,
edge_data_eltype=Symbol,
)
g[1st] = "v_a"
g[2nd] = "v_b"
g[3rd] = "v_c"
g[1st => 2nd] = :e_ab
g[2nd => 3rd] = :e_bc
@test g["a"] == "v_a"
@test g["b"] == "v_b"
@test g["c"] == "v_c"
@test g["a" => "b"] === :e_ab
@test g["b" => "a"] === :e_ab
@test g["b" => "c"] === :e_bc
@test g["c" => "b"] === :e_bc
@test g[1st] == "v_a"
@test g[1th] == "v_a"
@test g[2nd] == "v_b"
@test g[2th] == "v_b"
@test g[3rd] == "v_c"
@test g[3th] == "v_c"
@test g[1st => 2nd] === :e_ab
@test g[2nd => 1st] === :e_ab
@test g[2nd => 3rd] === :e_bc
@test g[3rd => 2nd] === :e_bc
end
end
end

0 comments on commit 3d2cb9c

Please sign in to comment.