-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimized.go
99 lines (90 loc) · 2.03 KB
/
optimized.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"sort"
)
func main() {
offset := 0
buf := make([]byte, 64*1024)
counts := make(map[string]*int)
for {
// Read input in 64KB blocks till EOF.
n, err := os.Stdin.Read(buf[offset:])
if err != nil && err != io.EOF {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if n == 0 {
break
}
// Offset remaining from last time plus number of bytes read.
chunk := buf[:offset+n]
// Find last end-of-line character in block read.
lastLF := bytes.LastIndexByte(chunk, '\n')
toProcess := chunk
if lastLF != -1 {
toProcess = chunk[:lastLF]
}
// Loop through toProcess slice and count words.
start := -1 // start -1 means in whitespace run
for i, c := range toProcess {
// Convert to ASCII lowercase in place as we go.
if c >= 'A' && c <= 'Z' {
c = c + ('a' - 'A')
toProcess[i] = c
}
if start >= 0 {
// In a word, look for end of word (whitespace).
if c <= ' ' {
// Count this word!
increment(counts, toProcess[start:i])
start = -1
}
} else {
// In whitespace, look for start of word (non-space).
if c > ' ' {
start = i
}
}
}
// Count last word, if any.
if start >= 0 && start < len(toProcess) {
increment(counts, toProcess[start:])
}
// Copy remaining bytes (incomplete line) to start of buffer.
if lastLF != -1 {
remaining := chunk[lastLF+1:]
copy(buf, remaining)
offset = len(remaining)
} else {
offset = 0
}
}
var ordered []Count
for word, count := range counts {
ordered = append(ordered, Count{word, *count})
}
sort.Slice(ordered, func(i, j int) bool {
return ordered[i].Count > ordered[j].Count
})
for _, count := range ordered {
fmt.Println(string(count.Word), count.Count)
}
}
func increment(counts map[string]*int, word []byte) {
if p, ok := counts[string(word)]; ok {
// Word already in map, increment existing int via pointer.
*p++
return
}
// Word not in map, insert new int.
n := 1
counts[string(word)] = &n
}
type Count struct {
Word string
Count int
}