/
sorthead.go
445 lines (420 loc) · 10.1 KB
/
sorthead.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
441
442
443
444
445
/*
Faster replacement for `sort | head`.
Works orders of magnitude faster on large amount of data.
Shows partial results (the current top) while working.
Example usage: find out what takes your disk space (shows current top directories while still working):
du -ha | sorthead -hr
Usage of sorthead:
-<digits>: same as -N <digits> (this option must always come first)
-h, --human-numeric-sort=false: compare human readable numbers (e.g., 2K 1G)
-I, --interactive=false: interactive mode (it is the default when no -N given)
-k, --key=0: sort by field number N, not the whole string
-N, --lines=10: print the first N lines instead of the first 10 (in interactive mode default is window size)
-n, --numeric-sort=false: compare according to string numerical value
-r, --reverse=false: reverse the result of comparisons
*/
package main
import (
"fmt"
"github.com/nsf/termbox-go"
flag "github.com/ogier/pflag"
"io"
"log"
"os"
"runtime/pprof"
"time"
)
type numkeyType float64
// topval[0] and numkey[0] are for current string,
// the rest are current top values
var topval [][]byte
var keyStart, keyEnd []int
var numkey []numkeyType
// maximum len(topval) is toplen+1
// (element 0 for current value and elements 1..toplen for the top)
// TODO: panic if toplen is not positive
var toplen int
func init() {
topval = [][]byte{{}}
keyStart = []int{0}
keyEnd = []int{0}
numkey = make([]numkeyType, 1)
}
var suffix = map[byte]numkeyType{}
func init() {
m1 := numkeyType(1024)
cur := numkeyType(1)
for _, c := range []byte("KMGTPEZY") {
cur *= m1
suffix[c] = cur
}
suffix['k'] = suffix['K']
suffix['m'] = suffix['M']
}
func curToNum() (out numkeyType) {
afterDot := false
multiplier := numkeyType(1)
for _, char := range topval[0][keyStart[0]:keyEnd[0]] {
if char >= '0' && char <= '9' {
addition := numkeyType(char - '0')
if afterDot {
multiplier *= 0.1
out += multiplier * addition
} else {
out *= 10
out += addition
}
} else if '.' == char {
if afterDot {
break
}
afterDot = true
} else if flagHuman {
if multiplier, known := suffix[char]; known {
out *= multiplier
}
break
} else {
break
}
}
return
}
func strMore(n int) bool {
//if n >= len(topval) {
// tbclose()
// log.Fatalf("topval: %d >= %d", n, len(topval))
//}
a := topval[0][keyStart[0]:keyEnd[0]]
b := topval[n][keyStart[n]:keyEnd[n]]
for i := 0; i < len(a); i++ {
if i >= len(b) {
return true
}
if a[i] == b[i] {
continue
}
return a[i] > b[i]
}
return true
}
func xor(a, b bool) bool {
if a {
return !b
}
return b
}
func add() {
var curnum numkeyType
if numCmp {
curnum = curToNum()
}
pos := len(topval)
for i := len(topval) - 1; i > 0; i-- {
numi := numkey[i]
if numCmp {
if xor(flagRev, curnum > numi) {
break
}
} else {
if xor(flagRev, strMore(i)) {
break
}
}
pos = i
}
if len(topval) < toplen+1 {
topval = append(topval, []byte{})
keyStart = append(keyStart, 0)
keyEnd = append(keyEnd, 0)
numkey = append(numkey, 0)
}
for i := len(topval) - 1; i > pos; i-- {
copyVal(i, i-1)
}
if pos < len(topval) {
numkey[0] = curnum
copyVal(pos, 0)
}
}
func copyVal(to, from int) {
if from >= len(topval) || from < 0 || to >= len(topval) || to < 0 {
tbclose()
log.Fatalf("copyVal bad index: to=%d to=%d len=%d", to, from, len(topval))
}
keyStart[to] = keyStart[from]
keyEnd[to] = keyEnd[from]
numkey[to] = numkey[from]
copy(topval[to], topval[from])
if len(topval[to]) < len(topval[from]) {
topval[to] = append(topval[to], topval[from][len(topval[to]):]...)
} else if len(topval[to]) > len(topval[from]) {
topval[to] = topval[to][:len(topval[from])]
}
}
var buffer [1024]byte
var bufStart, bufEnd int // automatically 0
var input = []inputFile{}
type inputFile struct {
file io.Reader
name string
}
func readString() bool {
topval[0] = topval[0][0:0]
curlen := 0
inField := false
curFieldNum := 0
keyStart[0] = 0
keyEnd[0] = 0
for {
if bufEnd == bufStart {
if bufEnd > 0 {
bufStart, bufEnd = 0, 0
}
if 0 == len(input) {
if 0 == flagField {
keyStart[0] = 0
keyEnd[0] = curlen
} else if keyEnd[0] < keyStart[0] {
keyEnd[0] = curlen
}
return len(topval[0]) > 0
}
n, err := input[0].file.Read(buffer[bufEnd:])
if n > 0 {
bufEnd += n
doneBytes += int64(n)
} else if io.EOF == err {
input = input[1:]
curFileStrings = 0
continue
} else {
tbclose()
log.Fatalln("read error:", err)
}
}
curByte := buffer[bufStart]
bufStart++
if flagField != 0 {
gotWhitespace := ' ' == curByte || '\t' == curByte || '\n' == curByte
if inField && gotWhitespace { // end of field
inField = false
if curFieldNum == flagField {
keyEnd[0] = curlen
}
} else if (!inField) && (!gotWhitespace) { // start of field
inField = true
curFieldNum++
if curFieldNum == flagField {
keyStart[0] = curlen
}
}
}
if '\n' == curByte {
if 0 == flagField {
keyStart[0] = 0
keyEnd[0] = curlen
}
return true
}
// this append() takes more than half of program run time
topval[0] = append(topval[0], curByte)
curlen++
}
tbclose()
panic("")
}
var flagNum, flagHuman, flagRev, flagInteractive bool
var flagField int
var doneBytes, doneStrings, curFileStrings int64
var inTermbox, numCmp bool
var started = time.Now()
func main() {
dashDigits("-N")
var cpuprofile *string
// Uncomment the following line to enable --cpuprofile option:
//cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
flag.BoolVarP(&flagNum, "numeric-sort", "n", false, "compare according to string numerical value")
flag.BoolVarP(&flagHuman, "human-numeric-sort", "h", false, "compare human readable numbers (e.g., 2K 1G)")
flag.BoolVarP(&flagRev, "reverse", "r", false, "reverse the result of comparisons")
flag.BoolVarP(&flagInteractive, "interactive", "I", false, "interactive mode (it is the default when no -N given)")
flag.IntVarP(&toplen, "lines", "N", 10, "print the first N lines instead of the first 10 (in interactive mode default is window size)")
flag.IntVarP(&flagField, "key", "k", 0, "sort by field number N, not the whole string")
flag.Parse()
numCmp = flagNum || flagHuman
if flagNum && flagHuman {
tbclose()
log.Fatalln("options `-hn' are incompatible")
}
flagGiven := map[string]bool{}
flag.Visit(func(pf *flag.Flag) {
flagGiven[pf.Name] = true
})
if !flagGiven["interactive"] && !flagGiven["lines"] {
flagInteractive = true
}
if toplen < 1 {
tbclose()
log.Fatalf("-N must have positive argument")
}
if nil != cpuprofile && *cpuprofile != "" {
pf, err := os.Create(*cpuprofile)
if err != nil {
tbclose()
log.Fatalf("cannot create %s: %s", *cpuprofile, err)
}
pprof.StartCPUProfile(pf)
defer pprof.StopCPUProfile()
}
inputFilenames := flag.Args()
if 0 == len(inputFilenames) {
inputFilenames = []string{"-"}
useShortHeader = true
}
for _, name := range inputFilenames {
file := os.Stdin
if "-" != name {
var err error
file, err = os.Open(name)
if nil != err {
tbclose()
log.Fatalln(err)
}
}
input = append(input, inputFile{file: file, name: name})
}
chStop := make(chan bool)
chDone := make(chan struct{})
if flagInteractive {
if err := termbox.Init(); err != nil {
tbclose()
log.Fatalln("Cannot initialize termbox", err)
}
inTermbox = true
if !flagGiven["lines"] {
_, ysize := termbox.Size()
toplen = ysize - 4
if toplen < 10 {
toplen = 10
}
}
go drawer(chStop, chDone)
}
for readString() {
add()
doneStrings++
curFileStrings++
}
if flagInteractive {
chStop <- true
<-chDone
}
finalOutput(0)
}
func finalOutput(code int) {
for _, str := range topval[1:] {
fmt.Println(string(str))
}
if 0 != code {
log.Println("Warning: command interrupted, not all data processed")
}
pprof.StopCPUProfile()
os.Exit(code)
}
func isDigit(ch byte) bool {
return byte('0') <= ch && ch <= byte('9')
}
func dashDigits(option string) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " -<digits>: same as %s <digits> (this option must always come first)\n", option)
flag.PrintDefaults()
}
if len(os.Args) <= 1 {
return
}
arg1 := []byte(os.Args[1])
if len(arg1) < 2 || '-' != arg1[0] || !isDigit(arg1[1]) {
return
}
for i, ch := range arg1[1:] {
if !isDigit(ch) {
tbclose()
log.Fatalf("bad dash-digits syntax: non digit characters %#v after %#v",
string(arg1[i+1:]), string(arg1[:i+1]))
}
}
os.Args = append(os.Args, "")
copy(os.Args[3:], os.Args[2:])
os.Args[1] = option
os.Args[2] = string(arg1[1:])
}
func drawer(chStop chan bool, chDone chan struct{}) {
chTimer := make(chan struct{})
go timer(chTimer)
go poller(chStop)
drawOnce()
for {
select {
case allDone := <-chStop:
tbclose()
if allDone {
chDone <- struct{}{}
return
}
finalOutput(1)
case <-chTimer:
drawOnce()
}
}
}
var drawHeader = "Processed %d strings (%d strings in file %#v) in %.1f seconds. Hit any key to interrupt."
var drawShortHeader = "Processed %d strings in %.1f seconds. Hit any key to interrupt."
var useShortHeader bool
func drawOnce() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
xsize, ysize := termbox.Size()
for i := 0; i < ysize && i < len(topval); i++ {
var str []rune
if 0 != i {
str = []rune(string(topval[i]))
} else if useShortHeader {
str = []rune(fmt.Sprintf(drawShortHeader, doneStrings,
time.Since(started).Seconds()))
} else {
str = []rune(fmt.Sprintf(drawHeader, doneStrings,
curFileStrings, input[0].name, time.Since(started).Seconds()))
}
for j := 0; j < xsize && j < len(str); j++ {
termbox.SetCell(j, i, str[j], termbox.ColorDefault, termbox.ColorDefault)
}
}
if err := termbox.Sync(); err != nil {
tbclose()
log.Fatalln("Cannot sync termbox:", err)
}
}
func timer(chTimer chan struct{}) {
for {
time.Sleep(time.Second)
chTimer <- struct{}{}
}
}
func poller(chStop chan bool) {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
chStop <- false
}
}
}
func tbclose() {
if inTermbox {
termbox.Close()
inTermbox = false
}
}
/*
TODO:
GNU sort options, including full -k POS syntax
*/