-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_writer.go
54 lines (44 loc) · 1.02 KB
/
dict_writer.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
package main
import (
"math/bits"
"parquet/float"
)
type floatDict struct {
dictValues []float32
indices map[float32]int32
values []int32
}
func NewFloatDict(nValues int) *floatDict {
return &floatDict{
dictValues: make([]float32, 0, nValues),
indices: make(map[float32]int32, nValues),
values: make([]int32, 0, nValues),
}
}
func (fd *floatDict) Write(v float32) {
index, ok := fd.indices[v]
if !ok {
index = int32(len(fd.dictValues))
fd.indices[v] = index
fd.dictValues = append(fd.dictValues, v)
}
fd.values = append(fd.values, index)
}
func (fd *floatDict) NDictValues() int {
return len(fd.dictValues)
}
func (fd *floatDict) DictBytes() []byte {
fw := float.NewWriter(len(fd.dictValues))
for _, v := range fd.dictValues {
fw.Write(v)
}
return fw.Bytes()
}
func (fd *floatDict) DataBytes() (int8, []byte) {
bitWidth := bits.Len(uint(len(fd.dictValues) - 1))
hw := newHybridWriter(len(fd.values), bitWidth)
for _, v := range fd.values {
hw.Write(v)
}
return int8(bitWidth), hw.Flush()
}