/
single.go
49 lines (43 loc) · 914 Bytes
/
single.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package elimination
import (
"gonum.org/v1/gonum/graph"
)
type Single struct {
G graph.DirectedBuilder
}
func (s *Single) Build(teams []Team) {
final := s.G.NewNode()
s.G.AddNode(final)
s.buildBracket(teams, final)
}
func (s *Single) buildBracket(teams []Team, next graph.Node) {
switch len(teams) {
case 0, 1, 2:
addTeams(next, teams)
case 3:
split := 2
s.buildMatch(teams[:split], next)
addTeams(next, teams[split:])
default:
split := len(teams) / 2
s.buildMatch(teams[:split], next)
s.buildMatch(teams[split:], next)
}
}
func (s *Single) buildMatch(teams []Team, next graph.Node) {
match := s.G.NewNode()
s.G.SetEdge(s.G.NewEdge(match, next))
s.buildBracket(teams, match)
}
func addTeams(n graph.Node, teams []Team) {
if len(teams) > 2 {
panic("elimination: too many teams")
}
match, ok := n.(Match)
if !ok {
return
}
for _, t := range teams {
match.AddTeam(t)
}
}