2017-10-21 18:39:23 +00:00
|
|
|
// Package gg implements ginger graph creation, traversal, and (de)serialization
|
|
|
|
package gg
|
|
|
|
|
|
|
|
import (
|
2018-01-21 15:39:25 +00:00
|
|
|
"crypto/rand"
|
2017-10-21 18:39:23 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2018-01-21 15:39:25 +00:00
|
|
|
"strings"
|
2017-10-21 18:39:23 +00:00
|
|
|
)
|
|
|
|
|
2018-01-21 15:39:25 +00:00
|
|
|
// Value wraps a go value in a way such that it will be uniquely identified
|
|
|
|
// within any Graph and between Graphs. Use NewValue to create a Value instance.
|
|
|
|
// You can create an instance manually as long as ID is globally unique.
|
|
|
|
type Value struct {
|
|
|
|
ID string
|
|
|
|
V interface{}
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 15:39:25 +00:00
|
|
|
// NewValue returns a Value instance wrapping any go value. The Value returned
|
|
|
|
// will be independent of the passed in go value. So if the same go value is
|
|
|
|
// passed in twice then the two returned Value instances will be treated as
|
|
|
|
// being different values by Graph.
|
|
|
|
func NewValue(V interface{}) Value {
|
2021-08-27 03:26:24 +00:00
|
|
|
b := make([]byte, 16)
|
2018-01-21 15:39:25 +00:00
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return Value{
|
|
|
|
ID: hex.EncodeToString(b),
|
|
|
|
V: V,
|
|
|
|
}
|
2017-11-24 18:05:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 21:49:22 +00:00
|
|
|
// VertexType enumerates the different possible vertex types.
|
2017-10-21 18:39:23 +00:00
|
|
|
type VertexType string
|
|
|
|
|
|
|
|
const (
|
2018-01-21 15:39:25 +00:00
|
|
|
// ValueVertex is a Vertex which contains exactly one value and has at least
|
2021-12-26 21:49:22 +00:00
|
|
|
// one edge (either input or output).
|
2018-01-21 15:39:25 +00:00
|
|
|
ValueVertex VertexType = "value"
|
2017-10-21 18:39:23 +00:00
|
|
|
|
2021-08-27 03:26:24 +00:00
|
|
|
// TupleVertex is a Vertex which contains two or more in edges and
|
2018-01-21 15:39:25 +00:00
|
|
|
// exactly one out edge
|
2021-08-27 03:26:24 +00:00
|
|
|
//
|
|
|
|
// TODO ^ what about 0 or 1 in edges?
|
|
|
|
TupleVertex VertexType = "tuple"
|
2017-10-21 18:39:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Edge is a uni-directional connection between two vertices with an attribute
|
2021-12-26 21:49:22 +00:00
|
|
|
// value.
|
2017-10-21 18:39:23 +00:00
|
|
|
type Edge struct {
|
|
|
|
From *Vertex
|
2018-01-21 15:39:25 +00:00
|
|
|
Value Value
|
2017-10-21 18:39:23 +00:00
|
|
|
To *Vertex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertex is a vertex in a Graph. No fields should be modified directly, only
|
2021-12-26 21:49:22 +00:00
|
|
|
// through method calls.
|
2017-10-21 18:39:23 +00:00
|
|
|
type Vertex struct {
|
2018-01-21 15:39:25 +00:00
|
|
|
ID string
|
2017-10-21 18:39:23 +00:00
|
|
|
VertexType
|
2018-01-21 15:39:25 +00:00
|
|
|
Value Value // Value is valid if-and-only-if VertexType is ValueVertex
|
2017-10-21 18:39:23 +00:00
|
|
|
In, Out []Edge
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
// OpenEdge is an un-realized Edge which can't be used for anything except
|
2017-10-21 18:39:23 +00:00
|
|
|
// constructing graphs. It has no meaning on its own.
|
2017-11-05 16:11:05 +00:00
|
|
|
type OpenEdge struct {
|
2017-10-21 18:39:23 +00:00
|
|
|
// fromV will be the source vertex as-if the vertex (and any sub-vertices of
|
|
|
|
// it) doesn't already exist in the graph. If it or it's sub-vertices does
|
|
|
|
// already that will need to be taken into account when persisting into the
|
|
|
|
// graph
|
|
|
|
fromV vertex
|
2018-01-21 15:39:25 +00:00
|
|
|
val Value
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 15:39:25 +00:00
|
|
|
func (oe OpenEdge) id() string {
|
|
|
|
return fmt.Sprintf("(%s,%s)", oe.fromV.id, oe.val.ID)
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// vertex is a representation of a vertex in the graph. Each Graph contains a
|
|
|
|
// set of all the Value vertex instances it knows about. Each of these contains
|
2017-11-05 16:11:05 +00:00
|
|
|
// all the input OpenEdges which are known for it. So you can think of these
|
|
|
|
// "top-level" Value vertex instances as root nodes in a tree, and each OpenEdge
|
2017-10-21 18:39:23 +00:00
|
|
|
// as a branch.
|
|
|
|
//
|
2017-11-05 16:11:05 +00:00
|
|
|
// If a OpenEdge contains a fromV which is a Value that vertex won't have its in
|
2021-08-27 03:26:24 +00:00
|
|
|
// slice populated no matter what. If fromV is a Tuple it will be populated,
|
2017-10-21 18:39:23 +00:00
|
|
|
// with any sub-Value's not being populated and so-on recursively
|
|
|
|
//
|
|
|
|
// When a view is constructed in makeView these Value instances are deduplicated
|
|
|
|
// and the top-level one's in value is used to properly connect it.
|
|
|
|
type vertex struct {
|
2018-01-21 15:39:25 +00:00
|
|
|
id string
|
2017-10-21 18:39:23 +00:00
|
|
|
VertexType
|
2018-01-21 15:39:25 +00:00
|
|
|
val Value
|
2017-11-05 16:11:05 +00:00
|
|
|
in []OpenEdge
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v vertex) cp() vertex {
|
|
|
|
cp := v
|
2017-11-05 16:11:05 +00:00
|
|
|
cp.in = make([]OpenEdge, len(v.in))
|
2017-10-21 18:39:23 +00:00
|
|
|
copy(cp.in, v.in)
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
func (v vertex) hasOpenEdge(oe OpenEdge) bool {
|
2018-01-21 15:39:25 +00:00
|
|
|
oeID := oe.id()
|
2017-10-21 18:39:23 +00:00
|
|
|
for _, in := range v.in {
|
2018-01-21 15:39:25 +00:00
|
|
|
if in.id() == oeID {
|
2017-10-21 18:39:23 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
func (v vertex) cpAndDelOpenEdge(oe OpenEdge) (vertex, bool) {
|
2018-01-21 15:39:25 +00:00
|
|
|
oeID := oe.id()
|
2017-10-22 17:39:32 +00:00
|
|
|
for i, in := range v.in {
|
2018-01-21 15:39:25 +00:00
|
|
|
if in.id() == oeID {
|
2017-10-22 17:39:32 +00:00
|
|
|
v = v.cp()
|
|
|
|
v.in = append(v.in[:i], v.in[i+1:]...)
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, false
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:49:22 +00:00
|
|
|
// Graph is a wrapper around a set of connected Vertices.
|
2017-10-21 18:39:23 +00:00
|
|
|
type Graph struct {
|
2017-12-03 19:38:53 +00:00
|
|
|
vM map[string]vertex // only contains value vertices
|
|
|
|
|
|
|
|
// generated by makeView on-demand
|
|
|
|
byVal map[string]*Vertex
|
|
|
|
all map[string]*Vertex
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 03:26:24 +00:00
|
|
|
// ZeroGraph is the root empty graph, and is the base off which all graphs are
|
|
|
|
// built.
|
|
|
|
var ZeroGraph = &Graph{
|
2017-12-03 19:38:53 +00:00
|
|
|
vM: map[string]vertex{},
|
|
|
|
byVal: map[string]*Vertex{},
|
|
|
|
all: map[string]*Vertex{},
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this does _not_ copy the view, as it's assumed the only reason to copy a
|
2021-12-26 21:49:22 +00:00
|
|
|
// graph is to modify it anyway.
|
2017-10-21 18:39:23 +00:00
|
|
|
func (g *Graph) cp() *Graph {
|
|
|
|
cp := &Graph{
|
|
|
|
vM: make(map[string]vertex, len(g.vM)),
|
|
|
|
}
|
2018-01-21 15:39:25 +00:00
|
|
|
for vID, v := range g.vM {
|
|
|
|
cp.vM[vID] = v
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Graph creation
|
|
|
|
|
2018-06-07 02:21:44 +00:00
|
|
|
func mkVertex(typ VertexType, val Value, ins ...OpenEdge) vertex {
|
2018-01-23 13:32:11 +00:00
|
|
|
v := vertex{VertexType: typ, in: ins}
|
|
|
|
switch typ {
|
|
|
|
case ValueVertex:
|
|
|
|
v.id = val.ID
|
|
|
|
v.val = val
|
2021-08-27 03:26:24 +00:00
|
|
|
case TupleVertex:
|
2018-01-23 13:32:11 +00:00
|
|
|
inIDs := make([]string, len(ins))
|
|
|
|
for i := range ins {
|
|
|
|
inIDs[i] = ins[i].id()
|
|
|
|
}
|
|
|
|
v.id = "[" + strings.Join(inIDs, ",") + "]"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown vertex type %q", typ))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
// ValueOut creates a OpenEdge which, when used to construct a Graph, represents
|
2018-01-21 15:39:25 +00:00
|
|
|
// an edge (with edgeVal attached to it) coming from the ValueVertex containing
|
2017-10-21 18:39:23 +00:00
|
|
|
// val.
|
|
|
|
//
|
2018-01-21 15:39:25 +00:00
|
|
|
// When constructing Graphs, Value vertices are de-duplicated on their Value. So
|
2017-11-05 16:11:05 +00:00
|
|
|
// multiple ValueOut OpenEdges constructed with the same val will be leaving the
|
2017-10-21 18:39:23 +00:00
|
|
|
// same Vertex instance in the constructed Graph.
|
2018-01-21 15:39:25 +00:00
|
|
|
func ValueOut(val, edgeVal Value) OpenEdge {
|
2018-06-07 02:21:44 +00:00
|
|
|
return OpenEdge{fromV: mkVertex(ValueVertex, val), val: edgeVal}
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 03:26:24 +00:00
|
|
|
// TupleOut creates an OpenEdge which, when used to construct a Graph,
|
2018-01-21 15:39:25 +00:00
|
|
|
// represents an edge (with edgeVal attached to it) coming from the
|
2021-08-27 03:26:24 +00:00
|
|
|
// TupleVertex comprised of the given ordered-set of input edges.
|
2017-10-21 18:39:23 +00:00
|
|
|
//
|
2021-08-27 03:26:24 +00:00
|
|
|
// When constructing Graphs Tuple vertices are de-duplicated on their input
|
|
|
|
// edges. So multiple Tuple OpenEdges constructed with the same set of input
|
|
|
|
// edges will be leaving the same Tuple instance in the constructed Graph.
|
|
|
|
func TupleOut(ins []OpenEdge, edgeVal Value) OpenEdge {
|
2017-11-05 16:11:05 +00:00
|
|
|
return OpenEdge{
|
2021-08-27 03:26:24 +00:00
|
|
|
fromV: mkVertex(TupleVertex, Value{}, ins...),
|
2018-01-23 13:32:11 +00:00
|
|
|
val: edgeVal,
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
// AddValueIn takes a OpenEdge and connects it to the Value Vertex containing
|
2017-10-22 17:39:32 +00:00
|
|
|
// val, returning the new Graph which reflects that connection. Any Vertices
|
2017-11-05 16:11:05 +00:00
|
|
|
// referenced within toe OpenEdge which do not yet exist in the Graph will also
|
2017-10-21 18:39:23 +00:00
|
|
|
// be created in this step.
|
2018-01-21 15:39:25 +00:00
|
|
|
func (g *Graph) AddValueIn(oe OpenEdge, val Value) *Graph {
|
2018-06-07 02:21:44 +00:00
|
|
|
to := mkVertex(ValueVertex, val)
|
2018-01-21 15:39:25 +00:00
|
|
|
toID := to.id
|
2017-10-21 18:39:23 +00:00
|
|
|
|
|
|
|
// if to is already in the graph, pull it out, as it might have existing in
|
|
|
|
// edges we want to keep
|
|
|
|
if exTo, ok := g.vM[toID]; ok {
|
|
|
|
to = exTo
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the incoming edge already exists in to then there's nothing to do
|
2017-11-05 16:11:05 +00:00
|
|
|
if to.hasOpenEdge(oe) {
|
2017-10-21 18:39:23 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
to = to.cp()
|
2017-11-05 16:11:05 +00:00
|
|
|
to.in = append(to.in, oe)
|
2017-10-21 18:39:23 +00:00
|
|
|
g = g.cp()
|
|
|
|
|
|
|
|
// starting with to (which we always overwrite) go through vM and
|
|
|
|
// recursively add in any vertices which aren't already there
|
|
|
|
var persist func(vertex)
|
|
|
|
persist = func(v vertex) {
|
2018-01-21 15:39:25 +00:00
|
|
|
if v.VertexType == ValueVertex {
|
|
|
|
vID := v.id
|
2017-10-21 18:39:23 +00:00
|
|
|
if _, ok := g.vM[vID]; !ok {
|
|
|
|
g.vM[vID] = v
|
|
|
|
}
|
2017-10-22 17:39:32 +00:00
|
|
|
} else {
|
|
|
|
for _, e := range v.in {
|
|
|
|
persist(e.fromV)
|
|
|
|
}
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(g.vM, toID)
|
|
|
|
persist(to)
|
2017-10-22 17:39:32 +00:00
|
|
|
for _, e := range to.in {
|
|
|
|
persist(e.fromV)
|
|
|
|
}
|
2017-10-21 18:39:23 +00:00
|
|
|
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
// DelValueIn takes a OpenEdge and disconnects it from the Value Vertex
|
2017-10-22 17:39:32 +00:00
|
|
|
// containing val, returning the new Graph which reflects the disconnection. If
|
|
|
|
// the Value Vertex doesn't exist within the graph, or it doesn't have the given
|
2017-11-05 16:11:05 +00:00
|
|
|
// OpenEdge, no changes are made. Any vertices referenced by toe OpenEdge for
|
2017-10-22 17:39:32 +00:00
|
|
|
// which that edge is their only outgoing edge will be removed from the Graph.
|
2018-01-21 15:39:25 +00:00
|
|
|
func (g *Graph) DelValueIn(oe OpenEdge, val Value) *Graph {
|
2018-06-07 02:21:44 +00:00
|
|
|
to := mkVertex(ValueVertex, val)
|
2018-01-21 15:39:25 +00:00
|
|
|
toID := to.id
|
2017-10-22 17:39:32 +00:00
|
|
|
|
|
|
|
// pull to out of the graph. if it's not there then bail
|
|
|
|
var ok bool
|
|
|
|
if to, ok = g.vM[toID]; !ok {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// get new copy of to without the half-edge, or return if the half-edge
|
|
|
|
// wasn't even in to
|
2017-11-05 16:11:05 +00:00
|
|
|
to, ok = to.cpAndDelOpenEdge(oe)
|
2017-10-22 17:39:32 +00:00
|
|
|
if !ok {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
g = g.cp()
|
|
|
|
g.vM[toID] = to
|
|
|
|
|
|
|
|
// connectedTo returns whether the vertex has any connections with the
|
|
|
|
// vertex of the given id, descending recursively
|
|
|
|
var connectedTo func(string, vertex) bool
|
|
|
|
connectedTo = func(vID string, curr vertex) bool {
|
|
|
|
for _, in := range curr.in {
|
2018-01-21 15:39:25 +00:00
|
|
|
if in.fromV.VertexType == ValueVertex && in.fromV.id == vID {
|
2017-10-22 17:39:32 +00:00
|
|
|
return true
|
2021-08-27 03:26:24 +00:00
|
|
|
} else if in.fromV.VertexType == TupleVertex && connectedTo(vID, in.fromV) {
|
2017-10-22 17:39:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isOrphaned returns whether the given vertex has any connections to other
|
|
|
|
// nodes in the graph
|
|
|
|
isOrphaned := func(v vertex) bool {
|
2018-01-21 15:39:25 +00:00
|
|
|
vID := v.id
|
2017-10-22 17:39:32 +00:00
|
|
|
if v, ok := g.vM[vID]; ok && len(v.in) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for vID2, v2 := range g.vM {
|
|
|
|
if vID2 == vID {
|
|
|
|
continue
|
|
|
|
} else if connectedTo(vID, v2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// if to is orphaned get rid of it
|
|
|
|
if isOrphaned(to) {
|
|
|
|
delete(g.vM, toID)
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:11:05 +00:00
|
|
|
// rmOrphaned descends down the given OpenEdge and removes any Value
|
2017-10-22 17:39:32 +00:00
|
|
|
// Vertices referenced in it which are now orphaned
|
2017-11-05 16:11:05 +00:00
|
|
|
var rmOrphaned func(OpenEdge)
|
|
|
|
rmOrphaned = func(oe OpenEdge) {
|
2018-01-21 15:39:25 +00:00
|
|
|
if oe.fromV.VertexType == ValueVertex && isOrphaned(oe.fromV) {
|
|
|
|
delete(g.vM, oe.fromV.id)
|
2021-08-27 03:26:24 +00:00
|
|
|
} else if oe.fromV.VertexType == TupleVertex {
|
2018-01-21 15:39:25 +00:00
|
|
|
for _, juncOe := range oe.fromV.in {
|
|
|
|
rmOrphaned(juncOe)
|
2017-10-22 17:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 16:11:05 +00:00
|
|
|
rmOrphaned(oe)
|
2017-10-22 17:39:32 +00:00
|
|
|
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union takes in another Graph and returns a new one which is the union of the
|
|
|
|
// two. Value vertices which are shared between the two will be merged so that
|
|
|
|
// the new vertex has the input edges of both.
|
2018-06-07 02:21:44 +00:00
|
|
|
//
|
2021-12-26 21:49:22 +00:00
|
|
|
// TODO it bothers me that the opposite of Disjoin is Union and not "Join".
|
2017-10-22 17:39:32 +00:00
|
|
|
func (g *Graph) Union(g2 *Graph) *Graph {
|
|
|
|
g = g.cp()
|
|
|
|
for vID, v2 := range g2.vM {
|
|
|
|
v, ok := g.vM[vID]
|
|
|
|
if !ok {
|
|
|
|
v = v2
|
|
|
|
} else {
|
|
|
|
for _, v2e := range v2.in {
|
2017-11-05 16:11:05 +00:00
|
|
|
if !v.hasOpenEdge(v2e) {
|
2017-10-22 17:39:32 +00:00
|
|
|
v.in = append(v.in, v2e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.vM[vID] = v
|
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
2017-10-21 18:39:23 +00:00
|
|
|
|
2018-06-07 02:21:44 +00:00
|
|
|
// Disjoin splits the Graph into as many independently connected Graphs as it
|
|
|
|
// contains. Each Graph returned will have vertices connected only within itself
|
|
|
|
// and not across to the other Graphs, and the Union of all returned Graphs will
|
|
|
|
// be the original again.
|
|
|
|
//
|
2021-08-27 03:26:24 +00:00
|
|
|
// The order of the Graphs returned is not deterministic. (TODO booooooo)
|
2018-06-07 02:21:44 +00:00
|
|
|
//
|
2021-08-27 03:26:24 +00:00
|
|
|
// ZeroGraph.Disjoin() returns empty slice.
|
2018-06-07 02:21:44 +00:00
|
|
|
func (g *Graph) Disjoin() []*Graph {
|
|
|
|
m := map[string]*Graph{} // maps each id to the Graph it belongs to
|
|
|
|
mG := map[*Graph]struct{}{} // tracks unique Graphs created
|
|
|
|
|
|
|
|
var connectedTo func(vertex) *Graph
|
|
|
|
connectedTo = func(v vertex) *Graph {
|
|
|
|
if v.VertexType == ValueVertex {
|
|
|
|
if g := m[v.id]; g != nil {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, oe := range v.in {
|
|
|
|
if g := connectedTo(oe.fromV); g != nil {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// used upon finding out that previously-thought-to-be disconnected vertices
|
2021-08-27 03:26:24 +00:00
|
|
|
// aren't. Merges the two graphs they're connected into one and updates all
|
|
|
|
// state internal to this function accordingly.
|
2018-06-07 02:21:44 +00:00
|
|
|
rejoin := func(gDst, gSrc *Graph) {
|
|
|
|
for id, v := range gSrc.vM {
|
|
|
|
gDst.vM[id] = v
|
|
|
|
m[id] = gDst
|
|
|
|
}
|
|
|
|
delete(mG, gSrc)
|
|
|
|
}
|
|
|
|
|
|
|
|
var connectTo func(vertex, *Graph)
|
|
|
|
connectTo = func(v vertex, g *Graph) {
|
|
|
|
if v.VertexType == ValueVertex {
|
|
|
|
if g2, ok := m[v.id]; ok && g != g2 {
|
|
|
|
rejoin(g, g2)
|
|
|
|
}
|
|
|
|
m[v.id] = g
|
|
|
|
}
|
|
|
|
for _, oe := range v.in {
|
|
|
|
connectTo(oe.fromV, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, v := range g.vM {
|
|
|
|
gV := connectedTo(v)
|
|
|
|
|
|
|
|
// if gV is nil it means this vertex is part of a new Graph which
|
|
|
|
// nothing else has been connected to yet.
|
|
|
|
if gV == nil {
|
2021-08-27 03:26:24 +00:00
|
|
|
gV = ZeroGraph.cp()
|
2018-06-07 02:21:44 +00:00
|
|
|
mG[gV] = struct{}{}
|
|
|
|
}
|
|
|
|
gV.vM[id] = v
|
|
|
|
|
|
|
|
// do this no matter what, because we want to descend in to the in edges
|
|
|
|
// and mark all of those as being part of this graph too
|
|
|
|
connectTo(v, gV)
|
|
|
|
}
|
|
|
|
|
|
|
|
gg := make([]*Graph, 0, len(mG))
|
|
|
|
for g := range mG {
|
|
|
|
gg = append(gg, g)
|
|
|
|
}
|
|
|
|
return gg
|
|
|
|
}
|
|
|
|
|
2017-10-21 18:39:23 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Graph traversal
|
|
|
|
|
|
|
|
func (g *Graph) makeView() {
|
2017-12-03 19:38:53 +00:00
|
|
|
if g.byVal != nil {
|
2017-10-21 18:39:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-03 19:38:53 +00:00
|
|
|
g.byVal = make(map[string]*Vertex, len(g.vM))
|
|
|
|
g.all = map[string]*Vertex{}
|
2017-10-21 18:39:23 +00:00
|
|
|
|
|
|
|
var getV func(vertex, bool) *Vertex
|
|
|
|
getV = func(v vertex, top bool) *Vertex {
|
2018-01-21 15:39:25 +00:00
|
|
|
V, ok := g.all[v.id]
|
2017-10-21 18:39:23 +00:00
|
|
|
if !ok {
|
2018-01-21 15:39:25 +00:00
|
|
|
V = &Vertex{ID: v.id, VertexType: v.VertexType, Value: v.val}
|
|
|
|
g.all[v.id] = V
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we can be sure all Value vertices will be called with top==true at
|
|
|
|
// some point, so we only need to descend into the input edges if:
|
|
|
|
// * top is true
|
2021-08-27 03:26:24 +00:00
|
|
|
// * this is a tuple's first time being gotten
|
|
|
|
if !top && (ok || v.VertexType != TupleVertex) {
|
2017-10-21 18:39:23 +00:00
|
|
|
return V
|
|
|
|
}
|
|
|
|
|
|
|
|
V.In = make([]Edge, 0, len(v.in))
|
|
|
|
for i := range v.in {
|
|
|
|
fromV := getV(v.in[i].fromV, false)
|
|
|
|
e := Edge{From: fromV, Value: v.in[i].val, To: V}
|
|
|
|
fromV.Out = append(fromV.Out, e)
|
|
|
|
V.In = append(V.In, e)
|
|
|
|
}
|
|
|
|
|
2018-01-21 15:39:25 +00:00
|
|
|
if v.VertexType == ValueVertex {
|
|
|
|
g.byVal[v.val.ID] = V
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return V
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range g.vM {
|
|
|
|
getV(v, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 15:39:25 +00:00
|
|
|
// ValueVertex returns the Value Vertex for the given value. If the Graph
|
2021-12-26 21:49:22 +00:00
|
|
|
// doesn't contain a vertex for the value then nil is returned.
|
2018-01-21 15:39:25 +00:00
|
|
|
func (g *Graph) ValueVertex(val Value) *Vertex {
|
2017-10-21 18:39:23 +00:00
|
|
|
g.makeView()
|
2018-01-21 15:39:25 +00:00
|
|
|
return g.byVal[val.ID]
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 21:49:22 +00:00
|
|
|
// ValueVertices returns all Value Vertices in the Graph.
|
2018-01-21 15:39:25 +00:00
|
|
|
func (g *Graph) ValueVertices() []*Vertex {
|
2017-10-22 17:39:32 +00:00
|
|
|
g.makeView()
|
2017-12-03 19:38:53 +00:00
|
|
|
vv := make([]*Vertex, 0, len(g.byVal))
|
|
|
|
for _, v := range g.byVal {
|
2017-10-22 17:39:32 +00:00
|
|
|
vv = append(vv, v)
|
|
|
|
}
|
|
|
|
return vv
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:49:22 +00:00
|
|
|
// Equal returns whether or not the two Graphs are equivalent in value.
|
2017-10-21 18:39:23 +00:00
|
|
|
func Equal(g1, g2 *Graph) bool {
|
|
|
|
if len(g1.vM) != len(g2.vM) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for v1ID, v1 := range g1.vM {
|
|
|
|
v2, ok := g2.vM[v1ID]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// since the vertices are values we must make sure their input sets are
|
|
|
|
// the same (which is tricky since they're unordered, unlike a
|
2021-08-27 03:26:24 +00:00
|
|
|
// tuple's)
|
2017-10-21 18:39:23 +00:00
|
|
|
if len(v1.in) != len(v2.in) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, in := range v1.in {
|
2017-11-05 16:11:05 +00:00
|
|
|
if !v2.hasOpenEdge(in) {
|
2017-10-21 18:39:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2017-11-05 16:57:57 +00:00
|
|
|
|
2018-03-03 17:32:40 +00:00
|
|
|
// TODO Walk, but by edge
|
|
|
|
// TODO Walk, but without end. AKA FSM
|
|
|
|
|
2018-06-07 02:21:44 +00:00
|
|
|
// Iter will iterate through the Graph's vertices, calling the callback on every
|
|
|
|
// Vertex in the Graph once. The vertex order used is non-deterministic. If the
|
|
|
|
// callback returns false the iteration is stopped.
|
|
|
|
func (g *Graph) Iter(callback func(*Vertex) bool) {
|
2017-11-05 16:57:57 +00:00
|
|
|
g.makeView()
|
2017-12-03 19:38:53 +00:00
|
|
|
if len(g.byVal) == 0 {
|
2017-11-05 16:57:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-03 19:38:53 +00:00
|
|
|
seen := make(map[*Vertex]bool, len(g.byVal))
|
2017-11-05 16:57:57 +00:00
|
|
|
var innerWalk func(*Vertex) bool
|
|
|
|
innerWalk = func(v *Vertex) bool {
|
|
|
|
if seen[v] {
|
|
|
|
return true
|
|
|
|
} else if !callback(v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
seen[v] = true
|
|
|
|
for _, e := range v.In {
|
|
|
|
if !innerWalk(e.From) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-12-03 19:38:53 +00:00
|
|
|
for _, v := range g.byVal {
|
2017-11-05 16:57:57 +00:00
|
|
|
if !innerWalk(v) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 19:38:53 +00:00
|
|
|
|
2021-12-26 21:49:22 +00:00
|
|
|
// ByID returns all vertices indexed by their ID field.
|
2017-12-03 19:38:53 +00:00
|
|
|
func (g *Graph) ByID() map[string]*Vertex {
|
|
|
|
g.makeView()
|
|
|
|
return g.all
|
|
|
|
}
|