-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution4.go
More file actions
201 lines (173 loc) · 4.06 KB
/
solution4.go
File metadata and controls
201 lines (173 loc) · 4.06 KB
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
package main
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"sort"
)
func processChuckS2(filePath string, fileOffset, fileSize int64, resultChan chan map[string]*WeatherStationStats) {
type hashTable struct {
key []byte
value *WeatherStationStats
}
const bucketsCount = 1 << 17 // number of hash buckets (power of 2)
items := make([]hashTable, bucketsCount)
size := 0
file, err := os.OpenFile(filePath, os.O_RDWR, 0666)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Seek(fileOffset, io.SeekStart)
if err != nil {
panic(err)
}
flr := io.LimitedReader{R: file, N: fileSize}
buf := make([]byte, 1024*1024) // allocate 1MB buffer to store file chunks
start := 0
for {
nb, err := flr.Read(buf[start:])
if err != nil && err != io.EOF {
panic(err)
}
if start+nb == 0 {
break
}
chunk := buf[:start+nb]
nl := bytes.LastIndexByte(chunk, '\n')
if nl < 0 {
break
}
left := chunk[nl+1:]
chunk = chunk[:nl+1]
for {
// FNV-1 constants from hash/fnv
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
// Hash the station name and look for ';'
var station, tempBytes []byte
hash := uint64(offset64)
i := 0
for ; i < len(chunk); i++ {
c := chunk[i]
if c == ';' {
station = chunk[:i]
tempBytes = chunk[i+1:]
break
}
hash ^= uint64(c)
hash *= prime64
}
if i == len(chunk) {
break
}
negative := false
idx := 0
if tempBytes[idx] == '-' {
negative = true
idx++
}
// Parse the first digit
tempFlt := float64(tempBytes[idx] - '0')
idx++
// Parse the second digit (optional).
if tempBytes[idx] != '.' {
tempFlt = tempFlt*10 + float64(tempBytes[idx]-'0')
idx++
}
idx++
tempFlt += float64(tempBytes[idx]-'0') / 10 // convert to decimal
if negative {
tempFlt = -tempFlt
}
chunk = tempBytes[idx:]
hashIdx := int(hash & uint64(bucketsCount-1))
for {
if items[hashIdx].key == nil {
// Found an empty slot, add new item
key := make([]byte, len(station))
copy(key, station)
items[hashIdx] = hashTable{
key: key,
value: &WeatherStationStats{
min: tempFlt,
max: tempFlt,
sum: tempFlt,
count: 1,
},
}
size++
break
}
if bytes.Equal(items[hashIdx].key, station) {
// Found matching slot, update stats
stat := items[hashIdx].value
stat.min = min(stat.min, tempFlt)
stat.max = max(stat.max, tempFlt)
stat.sum += tempFlt
stat.count++
break
}
// Another key already in slot, try next slot (linear probe)
hashIdx++
if hashIdx >= bucketsCount {
hashIdx = 0
}
}
}
start = copy(buf, left)
}
stats := make(map[string]*WeatherStationStats, size)
for _, item := range items {
if item.key == nil {
continue
}
stats[string(item.key)] = item.value
}
resultChan <- stats
}
func solution4(filePath string, output io.Writer) error {
maxGoroutines := runtime.NumCPU()
chunks, err := splitFile(filePath, maxGoroutines)
if err != nil {
return err
}
resultsChan := make(chan map[string]*WeatherStationStats)
for _, chunk := range chunks {
go processChuckS2(filePath, chunk.offset, chunk.size, resultsChan)
}
weatherData := make(map[string]*WeatherStationStats)
for i := 0; i < len(chunks); i++ {
for station, stat := range <-resultsChan {
ts := weatherData[station]
if ts == nil {
weatherData[station] = stat
continue
}
ts.min = min(ts.min, stat.min)
ts.max = max(ts.max, stat.max)
ts.sum += stat.sum
ts.count += stat.count
}
}
weatherStations := make([]string, 0, len(weatherData))
for station := range weatherData {
weatherStations = append(weatherStations, station)
}
sort.Strings(weatherStations)
fmt.Fprint(output, "{")
for i, station := range weatherStations {
if i > 0 {
fmt.Fprint(output, ", ")
}
stat := weatherData[station]
mean := stat.sum / float64(stat.count)
fmt.Fprintf(output, "%s=%.1f/%.1f/%.1f", station, stat.min, mean, stat.max)
}
fmt.Fprintln(output, "}")
return nil
}