This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
customhash.go
224 lines (202 loc) · 4.43 KB
/
customhash.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
package main
import (
// "bufio"
"bytes"
"fmt"
// "io/ioutil"
"io"
"os"
// "runtime/pprof"
"sort"
)
func main() {
// f, err := os.Create("cpuprofile")
// if err != nil {
// fmt.Fprintf(os.Stderr, "could not create CPU profile: %v\n", err)
// os.Exit(1)
// }
// if err := pprof.StartCPUProfile(f); err != nil {
// fmt.Fprintf(os.Stderr, "could not start CPU profile: %v\n", err)
// os.Exit(1)
// }
// defer pprof.StopCPUProfile()
buf := make([]byte, 64*1024)
offset := 0
var counts Counter
// counts := make(map[string]int)
for {
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
}
chunk := buf[:offset+n]
lastLF := bytes.LastIndexByte(chunk, '\n')
process := chunk
if lastLF != -1 {
process = chunk[:lastLF]
}
start := -1
for i, c := range process {
if c >= 'A' && c <= 'Z' {
c = c + ('a' - 'A')
process[i] = c
}
if start >= 0 {
if c == ' ' || c == '\n' {
counts.Count(process[start:i], 1)
// counts[string(process[start:i])]++
start = -1
}
} else {
if c != ' ' && c != '\n' {
start = i
}
}
}
if start >= 0 && start < len(process) {
counts.Count(process[start:], 1)
// counts[string(process[start:])]++
}
// for _, word := range bytes.Fields(toLower(process)) {
// counts[string(word)]++
// }
if lastLF == -1 {
offset = 0
} else {
remaining := chunk[lastLF+1:]
copy(buf, remaining)
offset = len(remaining)
}
}
ordered := counts.Items()
sort.Slice(ordered, func(i, j int) bool {
return ordered[i].Count > ordered[j].Count
})
for _, count := range ordered {
fmt.Printf("%s %d\n", string(count.Key), count.Count)
}
// 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.Printf("%s %d\n", count.word, count.count)
// }
}
type Counter struct {
items []CounterItem
size int
}
type CounterItem struct {
Key []byte
Count int
}
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
func (c *Counter) Count(word []byte, n int) {
// Like hash/fnv New64, Write, Sum64 -- but inlined
hash := uint64(offset64)
for _, c := range word {
hash *= prime64
hash ^= uint64(c)
}
h := int(hash & uint64(len(c.items)-1))
if c.size >= len(c.items)/2 {
// Current items more than half full, double items length
newLen := len(c.items) * 2
if newLen == 0 {
newLen = 1024
}
newC := Counter{items: make([]CounterItem, newLen)}
for _, item := range c.items {
if item.Key != nil {
newC.Count(item.Key, item.Count)
}
}
c.items = newC.items
h = int(hash & uint64(len(c.items)-1))
}
for {
if c.items[h].Key == nil {
// Found empty slot, insert
wordCopy := make([]byte, len(word))
copy(wordCopy, word)
c.items[h] = CounterItem{wordCopy, n}
c.size++
return
}
if bytes.Equal(c.items[h].Key, word) {
// Found matching slot, increment
c.items[h].Count += n
return
}
// Slot already holds another key, linear probe
h++
if h >= len(c.items) {
h = 0
}
}
}
func (c *Counter) Items() []CounterItem {
var items []CounterItem
for _, item := range c.items {
if item.Key != nil {
items = append(items, item)
}
}
return items
}
type Count struct {
word string
count int
}
// A split function for a Scanner that returns each space-separated word of
// text, with surrounding spaces deleted. It will never return an empty
// string. ASCII-only.
func scanWordsASCII(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Skip leading spaces.
start := 0
for ; start < len(data); start++ {
if !isSpace(data[start]) {
break
}
}
// Scan until space, marking end of word.
for i := start; i < len(data); i++ {
if isSpace(data[i]) {
return i + 1, toLower(data[start:i]), nil
}
}
// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
if atEOF && len(data) > start {
return len(data), toLower(data[start:]), nil
}
// Request more data.
return start, nil, nil
}
func isSpace(c byte) bool {
switch c {
case ' ', '\t', '\n', '\r':
return true
default:
return false
}
}
func toLower(s []byte) []byte {
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'A' && c <= 'Z' {
s[i] = c + ('a' - 'A')
}
}
return s
}