initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / support / goraph / bipartitegraph / bipartitegraph.go
1 package bipartitegraph
2
3 import "errors"
4 import "fmt"
5
6 import . "github.com/onsi/gomega/matchers/support/goraph/node"
7 import . "github.com/onsi/gomega/matchers/support/goraph/edge"
8
9 type BipartiteGraph struct {
10         Left  NodeOrderedSet
11         Right NodeOrderedSet
12         Edges EdgeSet
13 }
14
15 func NewBipartiteGraph(leftValues, rightValues []interface{}, neighbours func(interface{}, interface{}) (bool, error)) (*BipartiteGraph, error) {
16         left := NodeOrderedSet{}
17         for i, _ := range leftValues {
18                 left = append(left, Node{i})
19         }
20
21         right := NodeOrderedSet{}
22         for j, _ := range rightValues {
23                 right = append(right, Node{j + len(left)})
24         }
25
26         edges := EdgeSet{}
27         for i, leftValue := range leftValues {
28                 for j, rightValue := range rightValues {
29                         neighbours, err := neighbours(leftValue, rightValue)
30                         if err != nil {
31                                 return nil, errors.New(fmt.Sprintf("error determining adjacency for %v and %v: %s", leftValue, rightValue, err.Error()))
32                         }
33
34                         if neighbours {
35                                 edges = append(edges, Edge{left[i], right[j]})
36                         }
37                 }
38         }
39
40         return &BipartiteGraph{left, right, edges}, nil
41 }