Debian Codegraph @

topsort.go

raw symbols SCIP
1 /* {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2015
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE. }}} */
20
21 package topsort
22
23 import (
24 "errors"
25 )
26
27 // Network Helpers {{{
28
29 type Network struct {
30 nodes map[string]*Node
31 order []string
32 }
33
34 func NewNetwork() *Network {
35 return &Network{
36 nodes: map[string]*Node{},
37 order: []string{},
38 }
39 }
40
41 func (tn *Network) Sort() ([]*Node, error) {
42 nodes := make([]*Node, 0)
43 for _, key := range tn.order {
44 nodes = append(nodes, tn.nodes[key])
45 }
46 return sortNodes(nodes)
47 }
48
49 func (tn *Network) Get(name string) *Node {
50 node, _ := tn.nodes[name]
51 return node
52 }
53
54 func (tn *Network) AddNode(name string, value interface{}) *Node {
55 node := Node{
56 Name: name,
57 Value: value,
58 InboundEdges: make([]*Node, 0),
59 OutboundEdges: make([]*Node, 0),
60 Marked: false,
61 }
62
63 if _, ok := tn.nodes[name]; !ok {
64 tn.order = append(tn.order, name)
65 }
66 tn.nodes[name] = &node
67 return &node
68 }
69
70 // }}}
71
72 // Node Helpers {{{
73
74 type Node struct {
75 Name string
76 Value interface{}
77 OutboundEdges []*Node
78 InboundEdges []*Node
79 Marked bool
80 }
81
82 func (node *Node) IsCanidate() bool {
83 for _, edge := range node.InboundEdges {
84 /* for each node, let's check if they're all marked */
85 if !edge.Marked {
86 return false
87 }
88 }
89 return true
90 }
91
92 func (tn *Network) AddEdge(from string, to string) error {
93 fromNode := tn.Get(from)
94 toNode := tn.Get(to)
95
96 if fromNode == nil || toNode == nil {
97 return errors.New("Either the root or target node doesn't exist")
98 }
99
100 toNode.InboundEdges = append(toNode.InboundEdges, fromNode)
101 fromNode.OutboundEdges = append(fromNode.OutboundEdges, toNode)
102
103 return nil
104 }
105
106 func (tn *Network) AddEdgeAndNodes(from string, to string) error {
107 fromNode := tn.Get(from)
108 if fromNode == nil {
109 fromNode = tn.AddNode(from, nil)
110 }
111
112 toNode := tn.Get(to)
113 if toNode == nil {
114 toNode = tn.AddNode(to, nil)
115 }
116
117 return tn.AddEdge(from, to)
118 }
119
120 // }}}
121
122 // Sort Helpers {{{
123
124 func sortSingleNodes(nodes []*Node) ([]*Node, error) {
125 ret := make([]*Node, 0)
126 hasUnprunedNodes := false
127
128 for _, node := range nodes {
129 if node.Marked {
130 continue /* Already output. */
131 }
132
133 hasUnprunedNodes = true
134
135 /* Otherwise, let's see if we can prune it */
136 if node.IsCanidate() {
137 /* So, it has no deps and hasn't been marked; let's mark and
138 * output */
139 node.Marked = true
140 ret = append(ret, node)
141 }
142 }
143
144 if hasUnprunedNodes && len(ret) == 0 {
145 return nil, errors.New("Cycle detected :(")
146 }
147
148 return ret, nil
149 }
150
151 func sortNodes(nodes []*Node) (ret []*Node, err error) {
152 /* Reset Marked status of nodes so they're ready to sort */
153 for _, node := range nodes {
154 node.Marked = false
155 }
156 for {
157 generation, err := sortSingleNodes(nodes)
158 if err != nil {
159 return nil, err
160 }
161 if len(generation) == 0 {
162 break
163 }
164 ret = append(ret, generation...)
165 }
166 return
167 }
168
169 // }}}
170
171 // vim: foldmethod=marker