ITensor / DataGraphs.jl

A simple graph type with data on the vertices and edges.
MIT License
12 stars 3 forks source link

Generalize constructors beyond `Dictionary` for vertex data #11

Open mtfishman opened 1 year ago

mtfishman commented 1 year ago

For example:

julia> DataGraph(grid((4,)), ["X", "Y", "Z", "W"])
ERROR: MethodError: no method matching DataGraph(::SimpleGraph{Int64}, ::Vector{String})
Closest candidates are:
  DataGraph(::AbstractGraph) at ~/.julia/dev/DataGraphs/src/datagraph.jl:117
  DataGraph(::AbstractGraph, ::Dictionary) at ~/.julia/dev/DataGraphs/src/datagraph.jl:117
  DataGraph(::AbstractGraph, ::Dictionary, ::Dictionary) at ~/.julia/dev/DataGraphs/src/datagraph.jl:117
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[29]:1

This could automatically convert to a Dictionary, i.e. get converted internally to:

julia> DataGraph(grid((4,)), Dictionary(["X", "Y", "Z", "W"]))
DataGraph{Int64, String, Any, SimpleGraph{Int64}, Graphs.SimpleGraphs.SimpleEdge{Int64}} with 4 vertices:
Base.OneTo(4)

and 3 edge(s):
Edge 1 => 2
Edge 2 => 3
Edge 3 => 4

with vertex data:
4-element Dictionary{Int64, String}
 1 │ "X"
 2 │ "Y"
 3 │ "Z"
 4 │ "W"

and edge data:
0-element Dictionary{Graphs.SimpleGraphs.SimpleEdge{Int64}, Any}

This would only make sense for vertices with linear indexing.