65 lines
2.0 KiB
Go
65 lines
2.0 KiB
Go
/*
|
|
Copyright © 2025 filifa
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package dot
|
|
|
|
import (
|
|
"math"
|
|
|
|
igraph "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph"
|
|
|
|
"gonum.org/v1/gonum/graph"
|
|
"gonum.org/v1/gonum/graph/multi"
|
|
)
|
|
|
|
// DOTWeightedGraph is a graph to unmarshal DOT graphs.
|
|
type DOTWeightedGraph struct {
|
|
igraph.WeightedGraph
|
|
WeightAttribute string
|
|
}
|
|
|
|
// NewDOTDirectedGraph returns a graph with no nodes or edges.
|
|
func NewDOTDirectedGraph(weightAttr string) DOTWeightedGraph {
|
|
return DOTWeightedGraph{WeightedGraph: igraph.NewDirectedGraph(), WeightAttribute: weightAttr}
|
|
}
|
|
|
|
func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph {
|
|
return DOTWeightedGraph{WeightedGraph: igraph.NewUndirectedGraph(), WeightAttribute: weightAttr}
|
|
}
|
|
|
|
// NewLine returns a DOT-aware weighted line.
|
|
func (g DOTWeightedGraph) NewLine(from, to graph.Node) graph.Line {
|
|
var defaultWeight float64
|
|
if g.WeightAttribute == "" {
|
|
defaultWeight = 1
|
|
} else {
|
|
defaultWeight = math.NaN()
|
|
}
|
|
|
|
e := g.WeightedGraph.NewWeightedLine(from, to, defaultWeight).(multi.WeightedLine)
|
|
return &weightedLine{WeightedLine: e, weightAttribute: g.WeightAttribute}
|
|
}
|
|
|
|
// NewNode returns a DOT-aware Node.
|
|
func (g DOTWeightedGraph) NewNode() graph.Node {
|
|
return &Node{Node: g.WeightedGraph.NewNode()}
|
|
}
|
|
|
|
// SetLine adds a DOT-aware weighted line to the graph.
|
|
func (g DOTWeightedGraph) SetLine(e graph.Line) {
|
|
g.WeightedGraph.SetWeightedLine(e.(*weightedLine))
|
|
}
|