pgraph: Update graph API to remove Get prefix and add Adjacency
Simple cleanups.
This commit is contained in:
@@ -35,7 +35,7 @@ func (g *Graph) addEdgesByMatchingUIDS(v *Vertex, uids []resources.ResUID) []boo
|
|||||||
for _, uid := range uids {
|
for _, uid := range uids {
|
||||||
var found = false
|
var found = false
|
||||||
// uid is a ResUID object
|
// uid is a ResUID object
|
||||||
for _, vv := range g.GetVertices() { // search
|
for _, vv := range g.Vertices() { // search
|
||||||
if v == vv { // skip self
|
if v == vv { // skip self
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ func (g *Graph) addEdgesByMatchingUIDS(v *Vertex, uids []resources.ResUID) []boo
|
|||||||
// AutoEdges adds the automatic edges to the graph.
|
// AutoEdges adds the automatic edges to the graph.
|
||||||
func (g *Graph) AutoEdges() {
|
func (g *Graph) AutoEdges() {
|
||||||
log.Println("Compile: Adding AutoEdges...")
|
log.Println("Compile: Adding AutoEdges...")
|
||||||
for _, v := range g.GetVertices() { // for each vertexes autoedges
|
for _, v := range g.Vertices() { // for each vertexes autoedges
|
||||||
if !v.Meta().AutoEdge { // is the metaparam true?
|
if !v.Meta().AutoEdge { // is the metaparam true?
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ func (ag *baseGrouper) init(g *Graph) error {
|
|||||||
if ag.graph != nil {
|
if ag.graph != nil {
|
||||||
return fmt.Errorf("the init method has already been called")
|
return fmt.Errorf("the init method has already been called")
|
||||||
}
|
}
|
||||||
ag.graph = g // pointer
|
ag.graph = g // pointer
|
||||||
ag.vertices = ag.graph.GetVerticesSorted() // cache in deterministic order!
|
ag.vertices = ag.graph.VerticesSorted() // cache in deterministic order!
|
||||||
ag.i = 0
|
ag.i = 0
|
||||||
ag.j = 0
|
ag.j = 0
|
||||||
if len(ag.vertices) == 0 { // empty graph
|
if len(ag.vertices) == 0 { // empty graph
|
||||||
|
|||||||
@@ -273,9 +273,15 @@ func (g *Graph) NumEdges() int {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVertices returns a randomly sorted slice of all vertices in the graph
|
// Adjacency returns the adjacency map representing this graph. This is useful
|
||||||
|
// for users who which to operate on the raw data structure more efficiently.
|
||||||
|
func (g *Graph) Adjacency() map[*Vertex]map[*Vertex]*Edge {
|
||||||
|
return g.adjacency
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vertices returns a randomly sorted slice of all vertices in the graph.
|
||||||
// The order is random, because the map implementation is intentionally so!
|
// The order is random, because the map implementation is intentionally so!
|
||||||
func (g *Graph) GetVertices() []*Vertex {
|
func (g *Graph) Vertices() []*Vertex {
|
||||||
var vertices []*Vertex
|
var vertices []*Vertex
|
||||||
for k := range g.adjacency {
|
for k := range g.adjacency {
|
||||||
vertices = append(vertices, k)
|
vertices = append(vertices, k)
|
||||||
@@ -283,8 +289,8 @@ func (g *Graph) GetVertices() []*Vertex {
|
|||||||
return vertices
|
return vertices
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVerticesChan returns a channel of all vertices in the graph.
|
// VerticesChan returns a channel of all vertices in the graph.
|
||||||
func (g *Graph) GetVerticesChan() chan *Vertex {
|
func (g *Graph) VerticesChan() chan *Vertex {
|
||||||
ch := make(chan *Vertex)
|
ch := make(chan *Vertex)
|
||||||
go func(ch chan *Vertex) {
|
go func(ch chan *Vertex) {
|
||||||
for k := range g.adjacency {
|
for k := range g.adjacency {
|
||||||
@@ -302,9 +308,9 @@ func (vs VertexSlice) Len() int { return len(vs) }
|
|||||||
func (vs VertexSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
|
func (vs VertexSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
|
||||||
func (vs VertexSlice) Less(i, j int) bool { return vs[i].String() < vs[j].String() }
|
func (vs VertexSlice) Less(i, j int) bool { return vs[i].String() < vs[j].String() }
|
||||||
|
|
||||||
// GetVerticesSorted returns a sorted slice of all vertices in the graph
|
// VerticesSorted returns a sorted slice of all vertices in the graph
|
||||||
// The order is sorted by String() to avoid the non-determinism in the map type
|
// The order is sorted by String() to avoid the non-determinism in the map type
|
||||||
func (g *Graph) GetVerticesSorted() []*Vertex {
|
func (g *Graph) VerticesSorted() []*Vertex {
|
||||||
var vertices []*Vertex
|
var vertices []*Vertex
|
||||||
for k := range g.adjacency {
|
for k := range g.adjacency {
|
||||||
vertices = append(vertices, k)
|
vertices = append(vertices, k)
|
||||||
@@ -429,8 +435,8 @@ func (g *Graph) FilterGraph(name string, vertices []*Vertex) (*Graph, error) {
|
|||||||
return newGraph, nil
|
return newGraph, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisconnectedGraphs returns a list containing the N disconnected graphs.
|
// DisconnectedGraphs returns a list containing the N disconnected graphs.
|
||||||
func (g *Graph) GetDisconnectedGraphs() ([]*Graph, error) {
|
func (g *Graph) DisconnectedGraphs() ([]*Graph, error) {
|
||||||
graphs := []*Graph{}
|
graphs := []*Graph{}
|
||||||
var start *Vertex
|
var start *Vertex
|
||||||
var d []*Vertex // discovered
|
var d []*Vertex // discovered
|
||||||
@@ -438,7 +444,7 @@ func (g *Graph) GetDisconnectedGraphs() ([]*Graph, error) {
|
|||||||
for len(d) < c {
|
for len(d) < c {
|
||||||
|
|
||||||
// get an undiscovered vertex to start from
|
// get an undiscovered vertex to start from
|
||||||
for _, s := range g.GetVertices() {
|
for _, s := range g.Vertices() {
|
||||||
if !VertexContains(s, d) {
|
if !VertexContains(s, d) {
|
||||||
start = s
|
start = s
|
||||||
}
|
}
|
||||||
@@ -449,7 +455,7 @@ func (g *Graph) GetDisconnectedGraphs() ([]*Graph, error) {
|
|||||||
// filter all the collected elements into a new graph
|
// filter all the collected elements into a new graph
|
||||||
newgraph, err := g.FilterGraph(g.Name, dfs)
|
newgraph, err := g.FilterGraph(g.Name, dfs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errwrap.Wrapf(err, "could not run GetDisconnectedGraphs() properly")
|
return nil, errwrap.Wrapf(err, "could not run DisconnectedGraphs() properly")
|
||||||
}
|
}
|
||||||
// add number of elements found to found variable
|
// add number of elements found to found variable
|
||||||
d = append(d, dfs...) // extend
|
d = append(d, dfs...) // extend
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ func TestPgraphT6(t *testing.T) {
|
|||||||
G.AddEdge(v5, v6, e5)
|
G.AddEdge(v5, v6, e5)
|
||||||
//G.AddEdge(v6, v4, e6)
|
//G.AddEdge(v6, v4, e6)
|
||||||
|
|
||||||
graphs, err := G.GetDisconnectedGraphs()
|
graphs, err := G.DisconnectedGraphs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed with: %v", err)
|
t.Errorf("failed with: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user