mirror of
https://github.com/gonum/gonum.git
synced 2025-10-15 03:30:39 +08:00
graph/encoding: update for int64 IDs
This commit is contained in:
@@ -266,7 +266,7 @@ func (gen *generator) popSubgraph() []graph.Node {
|
|||||||
// unique returns the set of unique nodes contained within ns.
|
// unique returns the set of unique nodes contained within ns.
|
||||||
func unique(ns []graph.Node) []graph.Node {
|
func unique(ns []graph.Node) []graph.Node {
|
||||||
var nodes []graph.Node
|
var nodes []graph.Node
|
||||||
seen := make(set.Ints)
|
seen := make(set.Int64s)
|
||||||
for _, n := range ns {
|
for _, n := range ns {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
if seen.Has(id) {
|
if seen.Has(id) {
|
||||||
|
@@ -119,7 +119,7 @@ type printer struct {
|
|||||||
|
|
||||||
type edge struct {
|
type edge struct {
|
||||||
inGraph string
|
inGraph string
|
||||||
from, to int
|
from, to int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *printer) print(g graph.Graph, name string, needsIndent, isSubgraph bool) error {
|
func (p *printer) print(g graph.Graph, name string, needsIndent, isSubgraph bool) error {
|
||||||
|
@@ -13,9 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// intset is an integer set.
|
// intset is an integer set.
|
||||||
type intset map[int]struct{}
|
type intset map[int64]struct{}
|
||||||
|
|
||||||
func linksTo(i ...int) intset {
|
func linksTo(i ...int64) intset {
|
||||||
if len(i) == 0 {
|
if len(i) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -76,16 +76,17 @@ func undirectedGraphFrom(g []intset) graph.Graph {
|
|||||||
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
type namedNode struct {
|
type namedNode struct {
|
||||||
id int
|
id int64
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n namedNode) ID() int { return n.id }
|
func (n namedNode) ID() int64 { return n.id }
|
||||||
func (n namedNode) DOTID() string { return n.name }
|
func (n namedNode) DOTID() string { return n.name }
|
||||||
|
|
||||||
func directedNamedIDGraphFrom(g []intset) graph.Directed {
|
func directedNamedIDGraphFrom(g []intset) graph.Directed {
|
||||||
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
nu := namedNode{id: u, name: alpha[u : u+1]}
|
nu := namedNode{id: u, name: alpha[u : u+1]}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
nv := namedNode{id: v, name: alpha[v : v+1]}
|
nv := namedNode{id: v, name: alpha[v : v+1]}
|
||||||
@@ -98,6 +99,7 @@ func directedNamedIDGraphFrom(g []intset) graph.Directed {
|
|||||||
func undirectedNamedIDGraphFrom(g []intset) graph.Graph {
|
func undirectedNamedIDGraphFrom(g []intset) graph.Graph {
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
nu := namedNode{id: u, name: alpha[u : u+1]}
|
nu := namedNode{id: u, name: alpha[u : u+1]}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
nv := namedNode{id: v, name: alpha[v : v+1]}
|
nv := namedNode{id: v, name: alpha[v : v+1]}
|
||||||
@@ -108,24 +110,25 @@ func undirectedNamedIDGraphFrom(g []intset) graph.Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type attrNode struct {
|
type attrNode struct {
|
||||||
id int
|
id int64
|
||||||
name string
|
name string
|
||||||
attr []Attribute
|
attr []Attribute
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n attrNode) ID() int { return n.id }
|
func (n attrNode) ID() int64 { return n.id }
|
||||||
func (n attrNode) DOTAttributes() []Attribute { return n.attr }
|
func (n attrNode) DOTAttributes() []Attribute { return n.attr }
|
||||||
|
|
||||||
func directedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Directed {
|
func directedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Directed {
|
||||||
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := attrNode{id: u, attr: at}
|
nu := attrNode{id: u, attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
nv := attrNode{id: v, attr: at}
|
nv := attrNode{id: v, attr: at}
|
||||||
@@ -138,13 +141,14 @@ func directedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Directed {
|
|||||||
func undirectedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Graph {
|
func undirectedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Graph {
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := attrNode{id: u, attr: at}
|
nu := attrNode{id: u, attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
nv := attrNode{id: v, attr: at}
|
nv := attrNode{id: v, attr: at}
|
||||||
@@ -155,25 +159,26 @@ func undirectedNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type namedAttrNode struct {
|
type namedAttrNode struct {
|
||||||
id int
|
id int64
|
||||||
name string
|
name string
|
||||||
attr []Attribute
|
attr []Attribute
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n namedAttrNode) ID() int { return n.id }
|
func (n namedAttrNode) ID() int64 { return n.id }
|
||||||
func (n namedAttrNode) DOTID() string { return n.name }
|
func (n namedAttrNode) DOTID() string { return n.name }
|
||||||
func (n namedAttrNode) DOTAttributes() []Attribute { return n.attr }
|
func (n namedAttrNode) DOTAttributes() []Attribute { return n.attr }
|
||||||
|
|
||||||
func directedNamedIDNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Directed {
|
func directedNamedIDNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Directed {
|
||||||
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := namedAttrNode{id: u, name: alpha[u : u+1], attr: at}
|
nu := namedAttrNode{id: u, name: alpha[u : u+1], attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
nv := namedAttrNode{id: v, name: alpha[v : v+1], attr: at}
|
nv := namedAttrNode{id: v, name: alpha[v : v+1], attr: at}
|
||||||
@@ -186,13 +191,14 @@ func directedNamedIDNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Dire
|
|||||||
func undirectedNamedIDNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Graph {
|
func undirectedNamedIDNodeAttrGraphFrom(g []intset, attr [][]Attribute) graph.Graph {
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := namedAttrNode{id: u, name: alpha[u : u+1], attr: at}
|
nu := namedAttrNode{id: u, name: alpha[u : u+1], attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
nv := namedAttrNode{id: v, name: alpha[v : v+1], attr: at}
|
nv := namedAttrNode{id: v, name: alpha[v : v+1], attr: at}
|
||||||
@@ -216,6 +222,7 @@ func (e attrEdge) DOTAttributes() []Attribute { return e.attr }
|
|||||||
func directedEdgeAttrGraphFrom(g []intset, attr map[edge][]Attribute) graph.Directed {
|
func directedEdgeAttrGraphFrom(g []intset, attr map[edge][]Attribute) graph.Directed {
|
||||||
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
for v := range e {
|
for v := range e {
|
||||||
dg.SetEdge(attrEdge{from: simple.Node(u), to: simple.Node(v), attr: attr[edge{from: u, to: v}]})
|
dg.SetEdge(attrEdge{from: simple.Node(u), to: simple.Node(v), attr: attr[edge{from: u, to: v}]})
|
||||||
}
|
}
|
||||||
@@ -226,6 +233,7 @@ func directedEdgeAttrGraphFrom(g []intset, attr map[edge][]Attribute) graph.Dire
|
|||||||
func undirectedEdgeAttrGraphFrom(g []intset, attr map[edge][]Attribute) graph.Graph {
|
func undirectedEdgeAttrGraphFrom(g []intset, attr map[edge][]Attribute) graph.Graph {
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
for v := range e {
|
for v := range e {
|
||||||
dg.SetEdge(attrEdge{from: simple.Node(u), to: simple.Node(v), attr: attr[edge{from: u, to: v}]})
|
dg.SetEdge(attrEdge{from: simple.Node(u), to: simple.Node(v), attr: attr[edge{from: u, to: v}]})
|
||||||
}
|
}
|
||||||
@@ -266,13 +274,14 @@ func (e portedEdge) ToPort() (port, compass string) {
|
|||||||
func directedPortedAttrGraphFrom(g []intset, attr [][]Attribute, ports map[edge]portedEdge) graph.Directed {
|
func directedPortedAttrGraphFrom(g []intset, attr [][]Attribute, ports map[edge]portedEdge) graph.Directed {
|
||||||
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
dg := simple.NewDirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := attrNode{id: u, attr: at}
|
nu := attrNode{id: u, attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
pe := ports[edge{from: u, to: v}]
|
pe := ports[edge{from: u, to: v}]
|
||||||
@@ -287,13 +296,14 @@ func directedPortedAttrGraphFrom(g []intset, attr [][]Attribute, ports map[edge]
|
|||||||
func undirectedPortedAttrGraphFrom(g []intset, attr [][]Attribute, ports map[edge]portedEdge) graph.Graph {
|
func undirectedPortedAttrGraphFrom(g []intset, attr [][]Attribute, ports map[edge]portedEdge) graph.Graph {
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var at []Attribute
|
var at []Attribute
|
||||||
if u < len(attr) {
|
if u < int64(len(attr)) {
|
||||||
at = attr[u]
|
at = attr[u]
|
||||||
}
|
}
|
||||||
nu := attrNode{id: u, attr: at}
|
nu := attrNode{id: u, attr: at}
|
||||||
for v := range e {
|
for v := range e {
|
||||||
if v < len(attr) {
|
if v < int64(len(attr)) {
|
||||||
at = attr[v]
|
at = attr[v]
|
||||||
}
|
}
|
||||||
pe := ports[edge{from: u, to: v}]
|
pe := ports[edge{from: u, to: v}]
|
||||||
@@ -327,17 +337,18 @@ type structuredGraph struct {
|
|||||||
|
|
||||||
func undirectedStructuredGraphFrom(c []edge, g ...[]intset) graph.Graph {
|
func undirectedStructuredGraphFrom(c []edge, g ...[]intset) graph.Graph {
|
||||||
s := &structuredGraph{UndirectedGraph: simple.NewUndirectedGraph(0, math.Inf(1))}
|
s := &structuredGraph{UndirectedGraph: simple.NewUndirectedGraph(0, math.Inf(1))}
|
||||||
var base int
|
var base int64
|
||||||
for i, sg := range g {
|
for i, sg := range g {
|
||||||
sub := simple.NewUndirectedGraph(0, math.Inf(1))
|
sub := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range sg {
|
for u, e := range sg {
|
||||||
|
u := int64(u)
|
||||||
for v := range e {
|
for v := range e {
|
||||||
ce := simple.Edge{F: simple.Node(u + base), T: simple.Node(v + base)}
|
ce := simple.Edge{F: simple.Node(u + base), T: simple.Node(v + base)}
|
||||||
sub.SetEdge(ce)
|
sub.SetEdge(ce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.sub = append(s.sub, namedGraph{id: i, Graph: sub})
|
s.sub = append(s.sub, namedGraph{id: int64(i), Graph: sub})
|
||||||
base += len(sg)
|
base += int64(len(sg))
|
||||||
}
|
}
|
||||||
for _, e := range c {
|
for _, e := range c {
|
||||||
s.SetEdge(simple.Edge{F: simple.Node(e.from), T: simple.Node(e.to)})
|
s.SetEdge(simple.Edge{F: simple.Node(e.from), T: simple.Node(e.to)})
|
||||||
@@ -350,39 +361,41 @@ func (g structuredGraph) Structure() []Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type namedGraph struct {
|
type namedGraph struct {
|
||||||
id int
|
id int64
|
||||||
graph.Graph
|
graph.Graph
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g namedGraph) DOTID() string { return alpha[g.id : g.id+1] }
|
func (g namedGraph) DOTID() string { return alpha[g.id : g.id+1] }
|
||||||
|
|
||||||
type subGraph struct {
|
type subGraph struct {
|
||||||
id int
|
id int64
|
||||||
graph.Graph
|
graph.Graph
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g subGraph) ID() int { return g.id }
|
func (g subGraph) ID() int64 { return g.id }
|
||||||
func (g subGraph) Subgraph() graph.Graph {
|
func (g subGraph) Subgraph() graph.Graph {
|
||||||
return namedGraph(g)
|
return namedGraph(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
func undirectedSubGraphFrom(g []intset, s map[int][]intset) graph.Graph {
|
func undirectedSubGraphFrom(g []intset, s map[int64][]intset) graph.Graph {
|
||||||
var base int
|
var base int64
|
||||||
subs := make(map[int]subGraph)
|
subs := make(map[int64]subGraph)
|
||||||
for i, sg := range s {
|
for i, sg := range s {
|
||||||
sub := simple.NewUndirectedGraph(0, math.Inf(1))
|
sub := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range sg {
|
for u, e := range sg {
|
||||||
|
u := int64(u)
|
||||||
for v := range e {
|
for v := range e {
|
||||||
ce := simple.Edge{F: simple.Node(u + base), T: simple.Node(v + base)}
|
ce := simple.Edge{F: simple.Node(u + base), T: simple.Node(v + base)}
|
||||||
sub.SetEdge(ce)
|
sub.SetEdge(ce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
subs[i] = subGraph{id: i, Graph: sub}
|
subs[i] = subGraph{id: int64(i), Graph: sub}
|
||||||
base += len(sg)
|
base += int64(len(sg))
|
||||||
}
|
}
|
||||||
|
|
||||||
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
dg := simple.NewUndirectedGraph(0, math.Inf(1))
|
||||||
for u, e := range g {
|
for u, e := range g {
|
||||||
|
u := int64(u)
|
||||||
var nu graph.Node
|
var nu graph.Node
|
||||||
if sg, ok := subs[u]; ok {
|
if sg, ok := subs[u]; ok {
|
||||||
sg.id += base
|
sg.id += base
|
||||||
@@ -1264,7 +1277,7 @@ var encodeTests = []struct {
|
|||||||
|
|
||||||
// Handling subgraphs.
|
// Handling subgraphs.
|
||||||
{
|
{
|
||||||
g: undirectedSubGraphFrom(pageRankGraph, map[int][]intset{2: powerMethodGraph}),
|
g: undirectedSubGraphFrom(pageRankGraph, map[int64][]intset{2: powerMethodGraph}),
|
||||||
|
|
||||||
want: `graph {
|
want: `graph {
|
||||||
// Node definitions.
|
// Node definitions.
|
||||||
@@ -1315,7 +1328,7 @@ var encodeTests = []struct {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "H",
|
name: "H",
|
||||||
g: undirectedSubGraphFrom(pageRankGraph, map[int][]intset{1: powerMethodGraph}),
|
g: undirectedSubGraphFrom(pageRankGraph, map[int64][]intset{1: powerMethodGraph}),
|
||||||
strict: true,
|
strict: true,
|
||||||
|
|
||||||
want: `strict graph H {
|
want: `strict graph H {
|
||||||
|
Reference in New Issue
Block a user