diff --git a/Gopkg.lock b/Gopkg.lock index 5bac6963..338bef34 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -212,6 +212,14 @@ revision = "903d9455db9ff1d7ac1ab199062eca7266dd11a3" version = "v1.6.0" +[[projects]] + branch = "master" + digest = "1:7b5f2c7513992f4202e747a065620a4adaa94e331c4b415cfb92356a34c029e0" + name = "github.com/philopon/go-toposort" + packages = ["."] + pruneopts = "UT" + revision = "9be86dbd762f98b5b9a4eca110a3f40ef31d0375" + [[projects]] digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b" name = "github.com/pkg/errors" @@ -399,6 +407,7 @@ "github.com/logrusorgru/aurora", "github.com/onsi/ginkgo", "github.com/onsi/gomega", + "github.com/philopon/go-toposort", "github.com/pkg/errors", "github.com/spf13/cobra", "github.com/spf13/viper", diff --git a/vendor/github.com/philopon/go-toposort/.travis.yml b/vendor/github.com/philopon/go-toposort/.travis.yml new file mode 100644 index 00000000..a6a5866a --- /dev/null +++ b/vendor/github.com/philopon/go-toposort/.travis.yml @@ -0,0 +1,6 @@ +language: go +go: + - 1.5 + - 1.6 + - 1.7 + - master diff --git a/vendor/github.com/philopon/go-toposort/LICENSE b/vendor/github.com/philopon/go-toposort/LICENSE new file mode 100644 index 00000000..7e8f5cc0 --- /dev/null +++ b/vendor/github.com/philopon/go-toposort/LICENSE @@ -0,0 +1,7 @@ +Copyright 2017 Hirotomo Moriwaki + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/philopon/go-toposort/README.md b/vendor/github.com/philopon/go-toposort/README.md new file mode 100644 index 00000000..cea1197d --- /dev/null +++ b/vendor/github.com/philopon/go-toposort/README.md @@ -0,0 +1,53 @@ +go-toposort +== +[![Build Status](https://travis-ci.org/philopon/go-toposort.svg?branch=master)](https://travis-ci.org/philopon/go-toposort) +[![GoDoc](https://godoc.org/github.com/philopon/go-toposort?status.svg)](https://godoc.org/github.com/philopon/go-toposort) + +deterministic topological sort implementation for golang + +Example +-- + +```.go +package main + +import ( + "fmt" + + toposort "github.com/philopon/go-toposort" +) + +func main() { + graph := toposort.NewGraph(8) + graph.AddNodes("2", "3", "5", "7", "8", "9", "10", "11") + + graph.AddEdge("7", "8") + graph.AddEdge("7", "11") + + graph.AddEdge("5", "11") + + graph.AddEdge("3", "8") + graph.AddEdge("3", "10") + + graph.AddEdge("11", "2") + graph.AddEdge("11", "9") + graph.AddEdge("11", "10") + + graph.AddEdge("8", "9") + + result, ok := graph.Toposort() + if !ok { + panic("cycle detected") + } + + fmt.Println(result) +} +``` + +``` +[3 5 7 8 11 2 9 10] +``` + +License +-- +MIT diff --git a/vendor/github.com/philopon/go-toposort/toposort.go b/vendor/github.com/philopon/go-toposort/toposort.go new file mode 100644 index 00000000..270ca645 --- /dev/null +++ b/vendor/github.com/philopon/go-toposort/toposort.go @@ -0,0 +1,101 @@ +package toposort + +type Graph struct { + nodes []string + outputs map[string]map[string]int + inputs map[string]int +} + +func NewGraph(cap int) *Graph { + return &Graph{ + nodes: make([]string, 0, cap), + inputs: make(map[string]int), + outputs: make(map[string]map[string]int), + } +} + +func (g *Graph) AddNode(name string) bool { + g.nodes = append(g.nodes, name) + + if _, ok := g.outputs[name]; ok { + return false + } + g.outputs[name] = make(map[string]int) + g.inputs[name] = 0 + return true +} + +func (g *Graph) AddNodes(names ...string) bool { + for _, name := range names { + if ok := g.AddNode(name); !ok { + return false + } + } + return true +} + +func (g *Graph) AddEdge(from, to string) bool { + m, ok := g.outputs[from] + if !ok { + return false + } + + m[to] = len(m) + 1 + g.inputs[to]++ + + return true +} + +func (g *Graph) unsafeRemoveEdge(from, to string) { + delete(g.outputs[from], to) + g.inputs[to]-- +} + +func (g *Graph) RemoveEdge(from, to string) bool { + if _, ok := g.outputs[from]; !ok { + return false + } + g.unsafeRemoveEdge(from, to) + return true +} + +func (g *Graph) Toposort() ([]string, bool) { + L := make([]string, 0, len(g.nodes)) + S := make([]string, 0, len(g.nodes)) + + for _, n := range g.nodes { + if g.inputs[n] == 0 { + S = append(S, n) + } + } + + for len(S) > 0 { + var n string + n, S = S[0], S[1:] + L = append(L, n) + + ms := make([]string, len(g.outputs[n])) + for m, i := range g.outputs[n] { + ms[i-1] = m + } + + for _, m := range ms { + g.unsafeRemoveEdge(n, m) + + if g.inputs[m] == 0 { + S = append(S, m) + } + } + } + + N := 0 + for _, v := range g.inputs { + N += v + } + + if N > 0 { + return L, false + } + + return L, true +}