Skip to content

Commit

Permalink
Start using OrderedIndices as vertices of NamedGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 25, 2024
1 parent a0893ad commit 4de8d8f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/NamedGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module OrdinalIndexedDictionaries
module OrderedDictionaries
using Dictionaries: Dictionaries, AbstractIndices, Dictionary, gettoken

struct OrderedIndices{I} <: AbstractIndices{I}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
117 changes: 46 additions & 71 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,60 @@ 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

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)
Expand All @@ -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)
Expand All @@ -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}
Expand Down Expand Up @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion test/test_libs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ libs = [
:GraphsExtensions,
#:Keys,
#:NamedGraphGenerators,
:OrdinalIndexedDictionaries,
:OrderedDictionaries,
:OrdinalIndexing,
#:PartitionedGraphs,
#:SimilarType,
Expand Down

0 comments on commit 4de8d8f

Please sign in to comment.