From 4de8d8fd9b14f05d25467674ea6770ef95dc804a Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 25 Apr 2024 13:54:43 -0400 Subject: [PATCH] Start using OrderedIndices as vertices of NamedGraph --- src/NamedGraphs.jl | 2 +- .../src/OrderedDictionaries.jl} | 4 +- .../test/Project.toml | 0 .../test/runtests.jl | 2 +- src/namedgraph.jl | 117 +++++++----------- test/test_libs.jl | 2 +- 6 files changed, 52 insertions(+), 75 deletions(-) rename src/lib/{OrdinalIndexedDictionaries/src/OrdinalIndexedDictionaries.jl => OrderedDictionaries/src/OrderedDictionaries.jl} (96%) rename src/lib/{OrdinalIndexedDictionaries => OrderedDictionaries}/test/Project.toml (100%) rename src/lib/{OrdinalIndexedDictionaries => OrderedDictionaries}/test/runtests.jl (95%) diff --git a/src/NamedGraphs.jl b/src/NamedGraphs.jl index a004a77..2c99f05 100644 --- a/src/NamedGraphs.jl +++ b/src/NamedGraphs.jl @@ -2,7 +2,7 @@ module NamedGraphs include("lib/SimilarType/src/SimilarType.jl") include("lib/Keys/src/Keys.jl") include("lib/OrdinalIndexing/src/OrdinalIndexing.jl") -include("lib/OrdinalIndexedDictionaries/src/OrdinalIndexedDictionaries.jl") +include("lib/OrderedDictionaries/src/OrderedDictionaries.jl") include("lib/GraphGenerators/src/GraphGenerators.jl") include("lib/GraphsExtensions/src/GraphsExtensions.jl") include("utils.jl") diff --git a/src/lib/OrdinalIndexedDictionaries/src/OrdinalIndexedDictionaries.jl b/src/lib/OrderedDictionaries/src/OrderedDictionaries.jl similarity index 96% rename from src/lib/OrdinalIndexedDictionaries/src/OrdinalIndexedDictionaries.jl rename to src/lib/OrderedDictionaries/src/OrderedDictionaries.jl index b9198f1..c2726a4 100644 --- a/src/lib/OrdinalIndexedDictionaries/src/OrdinalIndexedDictionaries.jl +++ b/src/lib/OrderedDictionaries/src/OrderedDictionaries.jl @@ -1,4 +1,4 @@ -module OrdinalIndexedDictionaries +module OrderedDictionaries using Dictionaries: Dictionaries, AbstractIndices, Dictionary, gettoken struct OrderedIndices{I} <: AbstractIndices{I} @@ -15,6 +15,8 @@ struct OrderedIndices{I} <: AbstractIndices{I} end OrderedIndices(indices) = OrderedIndices{eltype(indices)}(indices) +OrderedIndices{I}(indices::OrderedIndices{I}) where {I} = copy(indices) + Base.@propagate_inbounds function Base.iterate(indices::OrderedIndices, state...) return iterate(indices.ordered_indices, state...) end diff --git a/src/lib/OrdinalIndexedDictionaries/test/Project.toml b/src/lib/OrderedDictionaries/test/Project.toml similarity index 100% rename from src/lib/OrdinalIndexedDictionaries/test/Project.toml rename to src/lib/OrderedDictionaries/test/Project.toml diff --git a/src/lib/OrdinalIndexedDictionaries/test/runtests.jl b/src/lib/OrderedDictionaries/test/runtests.jl similarity index 95% rename from src/lib/OrdinalIndexedDictionaries/test/runtests.jl rename to src/lib/OrderedDictionaries/test/runtests.jl index 4634b5b..0928c93 100644 --- a/src/lib/OrdinalIndexedDictionaries/test/runtests.jl +++ b/src/lib/OrderedDictionaries/test/runtests.jl @@ -1,6 +1,6 @@ @eval module $(gensym()) using Dictionaries: Dictionary -using NamedGraphs.OrdinalIndexedDictionaries: OrderedIndices, each_ordinal_index +using NamedGraphs.OrderedDictionaries: OrderedIndices, each_ordinal_index using NamedGraphs.OrdinalIndexing: th using Test: @test, @testset @testset "OrderedIndices" begin diff --git a/src/namedgraph.jl b/src/namedgraph.jl index 2b89f5c..e6811ed 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -13,36 +13,38 @@ using Graphs: using Graphs.SimpleGraphs: AbstractSimpleGraph, SimpleDiGraph, SimpleGraph using .GraphsExtensions: GraphsExtensions, vertextype, directed_graph_type, undirected_graph_type +using .OrderedDictionaries: OrderedIndices +using .OrdinalIndexing: th struct GenericNamedGraph{V,G<:AbstractSimpleGraph{Int}} <: AbstractNamedGraph{V} one_based_graph::G - ordered_vertices::Vector{V} - vertex_to_one_based_vertex::Dictionary{V,Int} + vertices::OrderedIndices{V} + global function _GenericNamedGraph(one_based_graph, vertices) + @assert length(vertices) == nv(one_based_graph) + return new{eltype(vertices),typeof(one_based_graph)}(one_based_graph, vertices) + end end # AbstractNamedGraph required interface. one_based_graph_type(G::Type{<:GenericNamedGraph}) = fieldtype(G, :one_based_graph) one_based_graph(graph::GenericNamedGraph) = getfield(graph, :one_based_graph) function vertex_to_one_based_vertex(graph::GenericNamedGraph, vertex) - return graph.vertex_to_one_based_vertex[vertex] + # TODO: Define a function `index_ordinal(ordinal_indices, index)`. + return vertices(graph).index_ordinals[vertex] end function one_based_vertex_to_vertex(graph::GenericNamedGraph, one_based_vertex::Integer) - return graph.ordered_vertices[one_based_vertex] + return vertices(graph)[one_based_vertex * th] end # TODO: Decide what this should output. -# Graphs.vertices(graph::GenericNamedGraph) = graph.ordered_vertices -Graphs.vertices(graph::GenericNamedGraph) = keys(graph.vertex_to_one_based_vertex) +Graphs.vertices(graph::GenericNamedGraph) = getfield(graph, :vertices) function Graphs.add_vertex!(graph::GenericNamedGraph, vertex) if vertex ∈ vertices(graph) return false end - add_vertex!(graph.one_based_graph) - # Update the forward map - push!(graph.ordered_vertices, vertex) - # Update the reverse map - insert!(graph.vertex_to_one_based_vertex, vertex, nv(graph.one_based_graph)) + add_vertex!(one_based_graph(graph)) + insert!(vertices(graph), vertex) return true end @@ -50,21 +52,21 @@ function Graphs.rem_vertex!(graph::GenericNamedGraph, vertex) if vertex ∉ vertices(graph) return false end - one_based_vertex = graph.vertex_to_one_based_vertex[vertex] - rem_vertex!(graph.one_based_graph, one_based_vertex) - # Insert the last vertex into the position of the vertex - # that is being deleted, then remove the last vertex. - last_vertex = last(graph.ordered_vertices) - graph.ordered_vertices[one_based_vertex] = last_vertex - last_vertex = pop!(graph.ordered_vertices) - graph.vertex_to_one_based_vertex[last_vertex] = one_based_vertex - delete!(graph.vertex_to_one_based_vertex, vertex) - return true + one_based_vertex = vertex_to_one_based_vertex(graph, vertex) + rem_vertex!(one_based_graph(graph), one_based_vertex) + delete!(vertices(graph), vertex) + ## # Insert the last vertex into the position of the vertex + ## # that is being deleted, then remove the last vertex. + ## last_vertex = last(graph.ordered_vertices) + ## graph.ordered_vertices[one_based_vertex] = last_vertex + ## last_vertex = pop!(graph.ordered_vertices) + ## graph.vertex_to_one_based_vertex[last_vertex] = one_based_vertex + ## delete!(graph.vertex_to_one_based_vertex, vertex) + ## return true end -function GraphsExtensions.rename_vertices(f::Function, g::GenericNamedGraph) - # TODO: Could be implemented as `set_vertices(g, f.(g.ordered_vertices))`. - return GenericNamedGraph(g.one_based_graph, f.(g.ordered_vertices)) +function GraphsExtensions.rename_vertices(f::Function, graph::GenericNamedGraph) + return GenericNamedGraph(one_based_graph(graph), f.(vertices(graph))) end function GraphsExtensions.rename_vertices(f::Function, g::AbstractSimpleGraph) @@ -79,69 +81,46 @@ function GraphsExtensions.convert_vertextype(vertextype::Type, graph::GenericNam ) end -# -# Convert inputs to vertex list -# - -function to_vertices(vertices) - return vec(collect(vertices)) -end -to_vertices(vertices::Vector) = vertices -to_vertices(vertices::Array) = vec(vertices) -# Treat tuple inputs as cartesian grid sizes -function to_vertices(vertices::Tuple{Vararg{Integer}}) - return vec(Tuple.(CartesianIndices(vertices))) -end -to_vertices(vertices::Integer) = to_vertices(Base.OneTo(vertices)) -function to_vertices(vertextype::Type, vertices) - return convert(Vector{vertextype}, to_vertices(vertices)) -end - # # Constructors from `AbstractSimpleGraph` # +to_vertices(vertices) = vertices +to_vertices(vertices::AbstractArray) = vec(vertices) +to_vertices(vertices::Integer) = Base.OneTo(vertices) + # Inner constructor +# TODO: Is this needed? function GenericNamedGraph{V,G}( - one_based_graph::AbstractSimpleGraph, vertices::Vector{V} -) where {V,G} - @assert length(vertices) == nv(one_based_graph) - # Need to copy the vertices here, otherwise the Dictionary uses a view of the vertices - return GenericNamedGraph{V,G}( - one_based_graph, vertices, Dictionary(copy(vertices), eachindex(vertices)) - ) + one_based_graph::G, vertices::OrderedIndices{V} +) where {V,G<:AbstractSimpleGraph{Int}} + return _GenericNamedGraph(one_based_graph, vertices) end -function GenericNamedGraph{V,G}(one_based_graph::AbstractSimpleGraph, vertices) where {V,G} - return GenericNamedGraph{V,G}(one_based_graph, to_vertices(V, vertices)) +function GenericNamedGraph{V,G}( + one_based_graph::AbstractSimpleGraph, vertices +) where {V,G<:AbstractSimpleGraph{Int}} + return GenericNamedGraph{V,G}( + convert(G, one_based_graph), OrderedIndices{V}(to_vertices(vertices)) + ) end function GenericNamedGraph{V}(one_based_graph::AbstractSimpleGraph, vertices) where {V} return GenericNamedGraph{V,typeof(one_based_graph)}(one_based_graph, vertices) end -function GenericNamedGraph{<:Any,G}( - one_based_graph::AbstractSimpleGraph, vertices::Vector -) where {G} - return GenericNamedGraph{eltype(vertices),G}(one_based_graph, vertices) -end - function GenericNamedGraph{<:Any,G}( one_based_graph::AbstractSimpleGraph, vertices -) where {G} - return GenericNamedGraph{<:Any,G}(one_based_graph, to_vertices(vertices)) +) where {G<:AbstractSimpleGraph{Int}} + return GenericNamedGraph{eltype(vertices),G}(one_based_graph, vertices) end -function GenericNamedGraph{<:Any,G}(one_based_graph::AbstractSimpleGraph) where {G} +function GenericNamedGraph{<:Any,G}(one_based_graph::AbstractSimpleGraph) where {G<:AbstractSimpleGraph{Int}} return GenericNamedGraph{<:Any,G}(one_based_graph, vertices(one_based_graph)) end -function GenericNamedGraph(one_based_graph::AbstractSimpleGraph, vertices::Vector) - return GenericNamedGraph{eltype(vertices)}(one_based_graph, vertices) -end - function GenericNamedGraph(one_based_graph::AbstractSimpleGraph, vertices) - return GenericNamedGraph(one_based_graph, to_vertices(vertices)) + return GenericNamedGraph{eltype(vertices)}(one_based_graph, vertices) end function GenericNamedGraph(one_based_graph::AbstractSimpleGraph) @@ -158,12 +137,8 @@ GenericNamedGraph{V,G}(graph::GenericNamedGraph{V,G}) where {V,G} = copy(graph) # Constructors from vertex names # -function GenericNamedGraph{V,G}(vertices::Vector{V}) where {V,G} - return GenericNamedGraph(G(length(vertices)), vertices) -end - function GenericNamedGraph{V,G}(vertices) where {V,G} - return GenericNamedGraph{V,G}(to_vertices(V, vertices)) + return GenericNamedGraph(G(length(vertices)), vertices) end function GenericNamedGraph{V}(vertices) where {V} @@ -194,7 +169,7 @@ GenericNamedGraph() = GenericNamedGraph(Any[]) # graph = set_one_based_graph(graph, copy(one_based_graph(graph))) # graph = set_vertices(graph, copy(vertices(graph))) function Base.copy(graph::GenericNamedGraph) - return GenericNamedGraph(copy(graph.one_based_graph), copy(graph.ordered_vertices)) + return GenericNamedGraph(copy(one_based_graph(graph)), copy(vertices(graph))) end Graphs.edgetype(G::Type{<:GenericNamedGraph}) = NamedEdge{vertextype(G)} diff --git a/test/test_libs.jl b/test/test_libs.jl index 4605e94..e47038c 100644 --- a/test/test_libs.jl +++ b/test/test_libs.jl @@ -6,7 +6,7 @@ libs = [ :GraphsExtensions, #:Keys, #:NamedGraphGenerators, - :OrdinalIndexedDictionaries, + :OrderedDictionaries, :OrdinalIndexing, #:PartitionedGraphs, #:SimilarType,