/usr/share/gocode/src/pault.ag/go/topsort/topsort.go is in golang-pault-go-topsort-dev 0.0~git20150602.0.a54e6e0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | /* {{{ Copyright (c) Paul R. Tagliamonte <paultag@gmail.com>, 2015
*
* 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. }}} */
package topsort
import (
"errors"
)
// Network Helpers {{{
type Network struct {
Nodes map[string]*Node
}
func NewNetwork() *Network {
return &Network{Nodes: map[string]*Node{}}
}
func (tn *Network) Sort() ([]*Node, error) {
nodes := make([]*Node, 0)
for _, v := range tn.Nodes {
nodes = append(nodes, v)
}
return sortNodes(nodes)
}
func (tn *Network) Get(name string) *Node {
return tn.Nodes[name]
}
func (tn *Network) AddNode(name string, value interface{}) *Node {
node := Node{
Name: name,
Value: value,
InboundEdges: make([]*Node, 0),
OutboundEdges: make([]*Node, 0),
Marked: false,
}
tn.Nodes[name] = &node
return &node
}
// }}}
// Node Helpers {{{
type Node struct {
Name string
Value interface{}
OutboundEdges []*Node
InboundEdges []*Node
Marked bool
}
func (node *Node) IsCanidate() bool {
for _, edge := range node.InboundEdges {
/* for each node, let's check if they're all marked */
if !edge.Marked {
return false
}
}
return true
}
func (tn *Network) AddEdge(from string, to string) error {
fromNode := tn.Get(from)
toNode := tn.Get(to)
if fromNode == nil || toNode == nil {
return errors.New("Either the root or target node doesn't exist")
}
toNode.InboundEdges = append(toNode.InboundEdges, fromNode)
fromNode.OutboundEdges = append(fromNode.OutboundEdges, toNode)
return nil
}
func (tn *Network) AddEdgeIfExists(from string, to string) {
tn.AddEdge(from, to)
}
// }}}
// Sort Helpers {{{
func sortSingleNodes(nodes []*Node) ([]*Node, error) {
ret := make([]*Node, 0)
hasUnprunedNodes := false
for _, node := range nodes {
if node.Marked {
continue /* Already output. */
}
hasUnprunedNodes = true
/* Otherwise, let's see if we can prune it */
if node.IsCanidate() {
/* So, it has no deps and hasn't been marked; let's mark and
* output */
node.Marked = true
ret = append(ret, node)
}
}
if hasUnprunedNodes && len(ret) == 0 {
return nil, errors.New("Cycle detected :(")
}
return ret, nil
}
func sortNodes(nodes []*Node) (ret []*Node, err error) {
for {
generation, err := sortSingleNodes(nodes)
if err != nil {
return nil, err
}
if len(generation) == 0 {
break
}
ret = append(ret, generation...)
}
return
}
// }}}
// vim: foldmethod=marker
|