2021-12-29 19:32:53 +00:00
|
|
|
// Package graph implements a generic directed graph type, with support for
|
|
|
|
// tuple vertices in addition to traditional "value" vertices.
|
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
// Value is any value which can be stored within a Graph. Values should be
|
|
|
|
// considered immutable, ie once used with the graph package their internal
|
|
|
|
// value does not change.
|
2021-12-29 19:32:53 +00:00
|
|
|
type Value interface {
|
|
|
|
Equal(Value) bool
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
// OpenEdge consists of the edge value (E) and source vertex value (V) of an
|
|
|
|
// edge in a Graph. When passed into the AddValueIn method a full edge is
|
|
|
|
// created. An OpenEdge can also be sourced from a tuple vertex, whose value is
|
|
|
|
// an ordered set of OpenEdges of this same type.
|
|
|
|
type OpenEdge[E, V Value] struct {
|
2021-12-29 19:55:45 +00:00
|
|
|
val *V
|
2021-12-30 16:56:20 +00:00
|
|
|
tup []*OpenEdge[E, V]
|
2021-12-29 19:55:45 +00:00
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
edgeVal E
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe *OpenEdge[E, V]) equal(oe2 *OpenEdge[E, V]) bool {
|
2021-12-29 19:55:45 +00:00
|
|
|
if !oe.edgeVal.Equal(oe2.edgeVal) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if oe.val != nil {
|
|
|
|
return oe2.val != nil && (*oe.val).Equal(*oe2.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(oe.tup) != len(oe2.tup) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range oe.tup {
|
|
|
|
if !oe.tup[i].equal(oe2.tup[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe *OpenEdge[E, V]) String() string {
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
vertexType := "tup"
|
|
|
|
|
2021-12-29 19:55:45 +00:00
|
|
|
var fromStr string
|
|
|
|
|
|
|
|
if oe.val != nil {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
vertexType = "val"
|
2021-12-29 19:55:45 +00:00
|
|
|
fromStr = (*oe.val).String()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
strs := make([]string, len(oe.tup))
|
|
|
|
|
|
|
|
for i := range oe.tup {
|
|
|
|
strs[i] = oe.tup[i].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
fromStr = fmt.Sprintf("[%s]", strings.Join(strs, ", "))
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:55:45 +00:00
|
|
|
return fmt.Sprintf("%s(%s, %s)", vertexType, fromStr, oe.edgeVal.String())
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithEdgeValue returns a copy of the OpenEdge with the given Value replacing
|
|
|
|
// the previous edge value.
|
|
|
|
//
|
|
|
|
// NOTE I _think_ this can be factored out once Graph is genericized.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe *OpenEdge[E, V]) WithEdgeValue(val E) *OpenEdge[E, V] {
|
2021-12-29 20:57:14 +00:00
|
|
|
oeCp := *oe
|
|
|
|
oeCp.edgeVal = val
|
|
|
|
return &oeCp
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EdgeValue returns the Value which lies on the edge itself.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe OpenEdge[E, V]) EdgeValue() E {
|
2021-12-29 19:32:53 +00:00
|
|
|
return oe.edgeVal
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromValue returns the Value from which the OpenEdge was created via ValueOut,
|
|
|
|
// or false if it wasn't created via ValueOut.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe OpenEdge[E, V]) FromValue() (V, bool) {
|
2021-12-29 19:55:45 +00:00
|
|
|
if oe.val == nil {
|
2021-12-29 19:32:53 +00:00
|
|
|
var zero V
|
|
|
|
return zero, false
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:55:45 +00:00
|
|
|
return *oe.val, true
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromTuple returns the tuple of OpenEdges from which the OpenEdge was created
|
|
|
|
// via TupleOut, or false if it wasn't created via TupleOut.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (oe OpenEdge[E, V]) FromTuple() ([]*OpenEdge[E, V], bool) {
|
2021-12-29 19:55:45 +00:00
|
|
|
if oe.val != nil {
|
2021-12-29 19:32:53 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:55:45 +00:00
|
|
|
return oe.tup, true
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValueOut creates a OpenEdge which, when used to construct a Graph, represents
|
2021-12-29 20:57:14 +00:00
|
|
|
// an edge (with edgeVal attached to it) coming from the vertex containing val.
|
2021-12-30 18:38:36 +00:00
|
|
|
func ValueOut[E, V Value](edgeVal E, val V) *OpenEdge[E, V] {
|
2021-12-30 16:56:20 +00:00
|
|
|
return &OpenEdge[E, V]{
|
2021-12-30 22:29:38 +00:00
|
|
|
val: &val,
|
2021-12-29 20:57:14 +00:00
|
|
|
edgeVal: edgeVal,
|
|
|
|
}
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TupleOut creates an OpenEdge which, when used to construct a Graph,
|
2021-12-29 20:57:14 +00:00
|
|
|
// represents an edge (with edgeVal attached to it) coming from the vertex
|
|
|
|
// comprised of the given ordered-set of input edges.
|
2021-12-30 18:38:36 +00:00
|
|
|
func TupleOut[E, V Value](edgeVal E, ins ...*OpenEdge[E, V]) *OpenEdge[E, V] {
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
if len(ins) == 1 {
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
var (
|
|
|
|
zero V
|
2021-12-30 22:29:38 +00:00
|
|
|
in = ins[0]
|
2021-12-29 20:57:14 +00:00
|
|
|
)
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
if edgeVal.Equal(zero) {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.edgeVal.Equal(zero) {
|
2021-12-29 20:57:14 +00:00
|
|
|
return in.WithEdgeValue(edgeVal)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
return &OpenEdge[E, V]{
|
2021-12-30 22:29:38 +00:00
|
|
|
tup: ins,
|
2021-12-29 19:32:53 +00:00
|
|
|
edgeVal: edgeVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
type graphValueIn[E, V Value] struct {
|
2021-12-30 22:29:38 +00:00
|
|
|
val V
|
2021-12-30 16:56:20 +00:00
|
|
|
edge *OpenEdge[E, V]
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (valIn graphValueIn[E, V]) equal(valIn2 graphValueIn[E, V]) bool {
|
2021-12-29 20:57:14 +00:00
|
|
|
return valIn.val.Equal(valIn2.val) && valIn.edge.equal(valIn2.edge)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Graph is an immutable container of a set of vertices. The Graph keeps track
|
2021-12-30 16:56:20 +00:00
|
|
|
// of all Values which terminate an OpenEdge. E indicates the type of edge
|
|
|
|
// values, while V indicates the type of vertex values.
|
2021-12-29 19:32:53 +00:00
|
|
|
//
|
|
|
|
// NOTE The current implementation of Graph is incredibly inefficient, there's
|
|
|
|
// lots of O(N) operations, unnecessary copying on changes, and duplicate data
|
|
|
|
// in memory.
|
2021-12-30 16:56:20 +00:00
|
|
|
type Graph[E, V Value] struct {
|
2021-12-30 22:29:38 +00:00
|
|
|
edges []*OpenEdge[E, V]
|
2021-12-30 16:56:20 +00:00
|
|
|
valIns []graphValueIn[E, V]
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (g *Graph[E, V]) cp() *Graph[E, V] {
|
|
|
|
cp := &Graph[E, V]{
|
2021-12-30 22:29:38 +00:00
|
|
|
edges: make([]*OpenEdge[E, V], len(g.edges)),
|
2021-12-30 16:56:20 +00:00
|
|
|
valIns: make([]graphValueIn[E, V], len(g.valIns)),
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
2021-12-29 20:57:14 +00:00
|
|
|
copy(cp.edges, g.edges)
|
2021-12-29 19:32:53 +00:00
|
|
|
copy(cp.valIns, g.valIns)
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (g *Graph[E, V]) String() string {
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
var strs []string
|
|
|
|
|
|
|
|
for _, valIn := range g.valIns {
|
2021-12-29 20:57:14 +00:00
|
|
|
strs = append(
|
|
|
|
strs,
|
|
|
|
fmt.Sprintf("valIn(%s, %s)", valIn.edge.String(), valIn.val.String()),
|
|
|
|
)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("graph(%s)", strings.Join(strs, ", "))
|
|
|
|
}
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
// NOTE this method is used more for its functionality than for any performance
|
|
|
|
// reasons... it's incredibly inefficient in how it deduplicates edges, but by
|
|
|
|
// doing the deduplication we enable the graph map operation to work correctly.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (g *Graph[E, V]) dedupeEdge(edge *OpenEdge[E, V]) *OpenEdge[E, V] {
|
2021-12-29 20:57:14 +00:00
|
|
|
|
|
|
|
// check if there's an existing edge which is fully equivalent in the graph
|
|
|
|
// already, and if so return that.
|
|
|
|
for i := range g.edges {
|
|
|
|
if g.edges[i].equal(edge) {
|
|
|
|
return g.edges[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this edge is a value edge then there's nothing else to do, return it.
|
|
|
|
if _, ok := edge.FromValue(); ok {
|
|
|
|
return edge
|
|
|
|
}
|
|
|
|
|
|
|
|
// this edge is a tuple edge, it's possible that one of its sub-edges is
|
|
|
|
// already in the graph. dedupe each sub-edge individually.
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
tupEdges := make([]*OpenEdge[E, V], len(edge.tup))
|
2021-12-29 20:57:14 +00:00
|
|
|
|
|
|
|
for i := range edge.tup {
|
|
|
|
tupEdges[i] = g.dedupeEdge(edge.tup[i])
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
return TupleOut(edge.EdgeValue(), tupEdges...)
|
2021-12-29 20:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
// ValueIns returns, if any, all OpenEdges which lead to the given Value in the
|
|
|
|
// Graph (ie, all those added via AddValueIn).
|
2021-12-29 20:57:14 +00:00
|
|
|
//
|
|
|
|
// The returned slice should not be modified.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (g *Graph[E, V]) ValueIns(val Value) []*OpenEdge[E, V] {
|
2021-12-29 20:57:14 +00:00
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
var edges []*OpenEdge[E, V]
|
2021-12-29 20:57:14 +00:00
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
for _, valIn := range g.valIns {
|
|
|
|
if valIn.val.Equal(val) {
|
2021-12-29 20:57:14 +00:00
|
|
|
edges = append(edges, valIn.edge)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
return edges
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddValueIn takes a OpenEdge and connects it to the Value vertex containing
|
|
|
|
// val, returning the new Graph which reflects that connection.
|
2021-12-30 18:38:36 +00:00
|
|
|
func (g *Graph[E, V]) AddValueIn(val V, oe *OpenEdge[E, V]) *Graph[E, V] {
|
2021-12-29 19:32:53 +00:00
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
valIn := graphValueIn[E, V]{
|
2021-12-30 22:29:38 +00:00
|
|
|
val: val,
|
2021-12-29 20:57:14 +00:00
|
|
|
edge: oe,
|
|
|
|
}
|
2021-12-29 19:32:53 +00:00
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
for i := range g.valIns {
|
|
|
|
if g.valIns[i].equal(valIn) {
|
2021-12-29 19:32:53 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
valIn.edge = g.dedupeEdge(valIn.edge)
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
g = g.cp()
|
|
|
|
g.valIns = append(g.valIns, valIn)
|
2021-12-29 20:57:14 +00:00
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns whether or not the two Graphs are equivalent in value.
|
2021-12-30 16:56:20 +00:00
|
|
|
func (g *Graph[E, V]) Equal(g2 *Graph[E, V]) bool {
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
if len(g.valIns) != len(g2.valIns) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for _, valIn := range g.valIns {
|
|
|
|
|
|
|
|
for _, valIn2 := range g2.valIns {
|
|
|
|
|
|
|
|
if valIn.equal(valIn2) {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2021-12-30 18:16:08 +00:00
|
|
|
|
|
|
|
func mapReduce[Ea, Va Value, Vb any](
|
|
|
|
root *OpenEdge[Ea, Va],
|
|
|
|
mapVal func(Va) (Vb, error),
|
|
|
|
reduceEdge func(*OpenEdge[Ea, Va], []Vb) (Vb, error),
|
|
|
|
) (
|
|
|
|
Vb, error,
|
2021-12-30 22:29:38 +00:00
|
|
|
) {
|
2021-12-30 18:16:08 +00:00
|
|
|
|
|
|
|
if valA, ok := root.FromValue(); ok {
|
|
|
|
|
|
|
|
valB, err := mapVal(valA)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
var zero Vb
|
|
|
|
return zero, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reduceEdge(root, []Vb{valB})
|
|
|
|
}
|
|
|
|
|
|
|
|
tupA, _ := root.FromTuple()
|
|
|
|
|
|
|
|
valsB := make([]Vb, len(tupA))
|
|
|
|
|
|
|
|
for i := range tupA {
|
|
|
|
|
|
|
|
valB, err := mapReduce[Ea, Va, Vb](
|
|
|
|
tupA[i], mapVal, reduceEdge,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
var zero Vb
|
|
|
|
return zero, err
|
|
|
|
}
|
|
|
|
|
|
|
|
valsB[i] = valB
|
|
|
|
}
|
|
|
|
|
|
|
|
return reduceEdge(root, valsB)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mappedVal[Va Value, Vb any] struct {
|
|
|
|
valA Va
|
|
|
|
valB Vb // result
|
|
|
|
}
|
|
|
|
|
|
|
|
type reducedEdge[Ea, Va Value, Vb any] struct {
|
|
|
|
edgeA *OpenEdge[Ea, Va]
|
2021-12-30 22:29:38 +00:00
|
|
|
valB Vb // result
|
2021-12-30 18:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapReduce recursively computes a resultant Value of type Vb from an
|
|
|
|
// OpenEdge[Ea, Va].
|
|
|
|
//
|
|
|
|
// Tuple edges which are encountered will have Reduce called on each OpenEdge
|
|
|
|
// branch of the tuple, to obtain a Vb for each branch. The edge value of the
|
|
|
|
// tuple edge (Ea) and the just obtained Vbs are then passed to reduceEdge to
|
|
|
|
// obtain a Vb for that edge.
|
|
|
|
//
|
|
|
|
// The values of value edges (Va) which are encountered are mapped to Vb using
|
|
|
|
// the mapVal function. The edge value of those value edges (Ea) and the just
|
|
|
|
// obtained Vb value are then passed to reduceEdge to obtain a Vb for that edge.
|
|
|
|
//
|
|
|
|
// If either the map or reduce function returns an error then processing is
|
|
|
|
// immediately cancelled and that error is returned directly.
|
|
|
|
//
|
|
|
|
// If a value or edge is connected to multiple times within the root OpenEdge it
|
|
|
|
// will only be mapped/reduced a single time, and the result of that single
|
|
|
|
// map/reduction will be passed to each dependant operation.
|
|
|
|
//
|
|
|
|
func MapReduce[Ea, Va Value, Vb any](
|
|
|
|
root *OpenEdge[Ea, Va],
|
|
|
|
mapVal func(Va) (Vb, error),
|
|
|
|
reduceEdge func(Ea, []Vb) (Vb, error),
|
|
|
|
) (
|
|
|
|
Vb, error,
|
2021-12-30 22:29:38 +00:00
|
|
|
) {
|
2021-12-30 18:16:08 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
zeroB Vb
|
|
|
|
|
|
|
|
// we use these to memoize reductions on values and edges, so a
|
|
|
|
// reduction is only performed a single time for each value/edge.
|
|
|
|
//
|
|
|
|
// NOTE this is not implemented very efficiently.
|
2021-12-30 22:29:38 +00:00
|
|
|
mappedVals []mappedVal[Va, Vb]
|
2021-12-30 18:16:08 +00:00
|
|
|
reducedEdges []reducedEdge[Ea, Va, Vb]
|
|
|
|
)
|
|
|
|
|
|
|
|
return mapReduce[Ea, Va, Vb](
|
|
|
|
root,
|
|
|
|
func(valA Va) (Vb, error) {
|
|
|
|
|
|
|
|
for _, mappedVal := range mappedVals {
|
|
|
|
if mappedVal.valA.Equal(valA) {
|
|
|
|
return mappedVal.valB, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valB, err := mapVal(valA)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return zeroB, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mappedVals = append(mappedVals, mappedVal[Va, Vb]{
|
|
|
|
valA: valA,
|
|
|
|
valB: valB,
|
|
|
|
})
|
|
|
|
|
|
|
|
return valB, nil
|
|
|
|
},
|
|
|
|
func(edgeA *OpenEdge[Ea, Va], valBs []Vb) (Vb, error) {
|
|
|
|
|
|
|
|
for _, reducedEdge := range reducedEdges {
|
|
|
|
if reducedEdge.edgeA.equal(edgeA) {
|
|
|
|
return reducedEdge.valB, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valB, err := reduceEdge(edgeA.EdgeValue(), valBs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return zeroB, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reducedEdges = append(reducedEdges, reducedEdge[Ea, Va, Vb]{
|
|
|
|
edgeA: edgeA,
|
2021-12-30 22:29:38 +00:00
|
|
|
valB: valB,
|
2021-12-30 18:16:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return valB, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|