This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
tsort.go
440 lines (350 loc) · 6.77 KB
/
tsort.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Go tsort - topological sort
Copyright (C) 2015 Eric Lagergren
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
"golang.org/x/sys/unix"
flag "github.com/ogier/pflag"
)
const (
Help = `Usage: tsort [OPTION] [FILE]
Write totally ordered list consistent with the partial ordering in FILE.
With no FILE, or when FILE is -, read standard input.
--help display this help and exit
--version output version information and exit
Report wc bugs to ericscottlagergren@gmail.com
Go coreutils home page: <https://www.github.com/EricLagergren/go-coreutils/>
`
Version = `tsort (Go coreutils) 1
Copyright (C) 2015 Eric Lagergren.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
`
)
var (
version = flag.BoolP("version", "v", false, "")
fatal = log.New(os.Stderr, "", 0)
)
type successor struct {
suc *item
next *successor
}
type item struct {
str string
left *item
right *item
balance int // -1, 0, or +1
count int64
qlink *item
top *successor
}
type action func(*item) bool
var (
head *item // Head of list
zeros *item // Tail of the list of 'zeros' (no predecessors)
loop *item // Used for loop detection
numStrings int64 // Strings to sort
)
func newItem(str string) *item { return &item{str: str} }
// Search binary tree for STR. If ROOT is nil, create a new tree.
func (root *item) searchItem(str string) *item {
var (
p = new(item)
q = new(item)
r = new(item)
s = new(item)
t = new(item)
)
if root.right == nil {
root.right = newItem(str)
return root.right
}
t = root
p = root.right
s = p
for {
a := strings.Compare(str, p.str)
if a == 0 {
return p
}
if a < 0 {
q = p.left
} else {
q = p.right
}
if q == nil {
q = newItem(str)
if a < 0 {
p.left = q
} else {
p.right = q
}
if strings.Compare(str, s.str) < 0 {
p = s.left
r = p
a = -1
} else {
p = s.right
r = p
a = 1
}
for p != q {
if strings.Compare(str, p.str) < 0 {
p.balance = -1
p = p.left
} else {
p.balance = 1
p = p.right
}
}
if s.balance == 0 || s.balance == -a {
s.balance += a
return q
}
if r.balance == a {
p = r
if a < 0 {
s.left = r.right
r.right = s
} else {
s.right = r.left
r.left = s
}
r.balance = 0
s.balance = r.balance
} else {
if a < 0 {
p = r.right
r.right = p.left
p.left = r
s.left = p.right
p.right = s
} else {
p = r.left
r.left = p.right
p.right = r
s.right = p.left
p.left = s
}
s.balance = 0
r.balance = 0
if p.balance == a {
s.balance = -a
} else if p.balance == -a {
r.balance = a
}
p.balance = 0
}
if s == t.right {
t.right = p
} else {
t.left = p
}
return q
}
if q.balance > 0 {
t = p
s = q
}
p = q
}
}
func recordRelation(j, k *item) {
if j.str != k.str {
k.count++
p := &successor{
suc: k,
next: j.top,
}
j.top = p
}
}
func countItems(_ *item) bool {
numStrings++
return false
}
func scanZeros(k *item) bool {
if k.count == 0 && k.str != "" {
if head == nil {
head = k
} else {
zeros.qlink = k
}
zeros = k
}
return false
}
// Try and detect loops. e.g.,
// 1 2
// 2 1
// If any are found, print to stderr.
func detectLoop(k *item) bool {
if k.count > 0 {
if loop == nil {
loop = k
} else {
p := &k.top
for *p != nil {
if (*p).suc == loop {
if k.qlink != nil {
for loop != nil {
tmp := loop.qlink
fatal.Printf("tsort: %s", loop.str)
if loop == k {
(*p).suc.count--
*p = (*p).next
break
}
loop.qlink = nil
loop = tmp
}
for loop != nil {
tmp := loop.qlink
loop.qlink = nil
loop = tmp
}
return true
}
k.qlink = loop
loop = k
break
}
p = &(*p).next
}
}
}
return false
}
// Recurse the tree at ROOT, calling ACTION for each node
func (root *item) recurseTree(fn action) bool {
if root.left == nil && root.right == nil {
return fn(root)
}
if root.left != nil {
if root.left.recurseTree(fn) {
return true
}
}
if fn(root) {
return true
}
if root.right != nil {
if root.right.recurseTree(fn) {
return true
}
}
return false
}
// Walk the tree at ROOT, calling ACTION for each node
func (root *item) walkTree(fn action) {
if root.right != nil {
root.right.recurseTree(fn)
}
}
func tsort(rw io.ReadWriter) int {
var (
root = newItem("")
j *item
k *item
ok int
)
scanner := bufio.NewScanner(rw)
scanner.Split(bufio.ScanWords)
// https://talks.golang.org/2015/tricks.slide#16
if file, ok := rw.(interface {
Fd() uintptr
}); ok {
unix.Fadvise(int(file.Fd()), 0, 0, unix.FADV_SEQUENTIAL)
}
for scanner.Scan() {
k = root.searchItem(scanner.Text())
if j != nil {
recordRelation(j, k)
k = nil
}
j = k
}
if k != nil {
fatal.Fatalln("input contains an odd number of tokens")
}
root.walkTree(countItems)
for numStrings > 0 {
root.walkTree(scanZeros)
for head != nil {
p := head.top
fmt.Fprintln(rw, head.str)
head.str = ""
numStrings--
for p != nil {
p.suc.count--
if p.suc.count == 0 {
zeros.qlink = p.suc
zeros = p.suc
}
p = p.next
}
head = head.qlink
}
if numStrings > 0 {
fatal.Print("tsort: input contains a loop:")
ok = 1
for {
root.walkTree(detectLoop)
if loop == nil {
break
}
}
}
}
return ok
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s", Help)
os.Exit(1)
}
flag.Parse()
if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
if flag.NArg() > 1 {
fatal.Fatalf("extra operand %s", flag.Arg(1))
}
file := os.Stdin
if (flag.Arg(0) != "-" &&
flag.Arg(0) != "") ||
flag.NArg() != 0 {
var err error
file, err = os.Open(flag.Arg(0))
if err != nil {
fatal.Fatalln(err)
}
defer file.Close()
}
os.Exit(tsort(struct {
io.Reader
io.Writer
}{
file,
os.Stdout,
}))
}