...

Source file src/github.com/onsi/gomega/matchers/support/goraph/bipartitegraph/bipartitegraph.go

Documentation: github.com/onsi/gomega/matchers/support/goraph/bipartitegraph

     1  package bipartitegraph
     2  
     3  import "fmt"
     4  
     5  import . "github.com/onsi/gomega/matchers/support/goraph/node"
     6  import . "github.com/onsi/gomega/matchers/support/goraph/edge"
     7  
     8  type BipartiteGraph struct {
     9  	Left  NodeOrderedSet
    10  	Right NodeOrderedSet
    11  	Edges EdgeSet
    12  }
    13  
    14  func NewBipartiteGraph(leftValues, rightValues []interface{}, neighbours func(interface{}, interface{}) (bool, error)) (*BipartiteGraph, error) {
    15  	left := NodeOrderedSet{}
    16  	for i, v := range leftValues {
    17  		left = append(left, Node{ID: i, Value: v})
    18  	}
    19  
    20  	right := NodeOrderedSet{}
    21  	for j, v := range rightValues {
    22  		right = append(right, Node{ID: j + len(left), Value: v})
    23  	}
    24  
    25  	edges := EdgeSet{}
    26  	for i, leftValue := range leftValues {
    27  		for j, rightValue := range rightValues {
    28  			neighbours, err := neighbours(leftValue, rightValue)
    29  			if err != nil {
    30  				return nil, fmt.Errorf("error determining adjacency for %v and %v: %s", leftValue, rightValue, err.Error())
    31  			}
    32  
    33  			if neighbours {
    34  				edges = append(edges, Edge{Node1: left[i].ID, Node2: right[j].ID})
    35  			}
    36  		}
    37  	}
    38  
    39  	return &BipartiteGraph{left, right, edges}, nil
    40  }
    41  
    42  // FreeLeftRight returns left node values and right node values
    43  // of the BipartiteGraph's nodes which are not part of the given edges.
    44  func (bg *BipartiteGraph) FreeLeftRight(edges EdgeSet) (leftValues, rightValues []interface{}) {
    45  	for _, node := range bg.Left {
    46  		if edges.Free(node) {
    47  			leftValues = append(leftValues, node.Value)
    48  		}
    49  	}
    50  	for _, node := range bg.Right {
    51  		if edges.Free(node) {
    52  			rightValues = append(rightValues, node.Value)
    53  		}
    54  	}
    55  	return
    56  }
    57  

View as plain text