Skip to content

Commit

Permalink
Faster has_vertex
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 12, 2024
1 parent 71aca02 commit 1433220
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
7 changes: 4 additions & 3 deletions src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ parent_graph(graph::AbstractNamedGraph) = not_implemented()
# ?
parent_graph_type(graph::AbstractNamedGraph) = not_implemented()

function Graphs.has_vertex(graph::AbstractNamedGraph, vertex)
return not_implemented()
end

parent_vertextype(graph::AbstractNamedGraph) = vertextype(parent_graph(graph))

# Convert vertex to parent vertex
Expand Down Expand Up @@ -137,9 +141,6 @@ function parent_edges_to_edges(graph::AbstractNamedGraph, parent_edges)
return map(parent_edge_to_edge(graph), parent_edges)
end

# TODO: This is `O(nv(g))`, use `haskey(vertex_to_parent_vertex(g), v)` instead?
has_vertex(g::AbstractNamedGraph, v) = v in vertices(g)

function edges(graph::AbstractNamedGraph)
return parent_edges_to_edges(graph, parent_edges(graph))
end
Expand Down
3 changes: 3 additions & 0 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ vertices(graph::GenericNamedGraph) = getfield(graph, :vertices)
function vertex_to_parent_vertex(graph::GenericNamedGraph, vertex)
return graph.vertex_to_parent_vertex[vertex]
end
function Graphs.has_vertex(graph::GenericNamedGraph, vertex)
return haskey(graph.vertex_to_parent_vertex, vertex)
end

function convert_vertextype(V::Type, graph::GenericNamedGraph)
return GenericNamedGraph(parent_graph(graph), convert(Vector{V}, vertices(graph)))
Expand Down

0 comments on commit 1433220

Please sign in to comment.