This repository is currently being migrated. It's locked while the migration is in progress.
forked from parsyl/parquet
/
main.go
202 lines (178 loc) · 3.67 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"log"
"os"
"text/template"
)
var (
pkg = flag.String("package", "main", "package of the generated code")
max = flag.Int("maxwidth", 3, "the bit width at which to stop")
outPth = flag.String("output", "bitpack.go", "name of the file that is produced, defaults to parquet.go")
)
func main() {
flag.Parse()
pb := bitback{Package: *pkg, Max: *max}
tmpl := template.New("output").Funcs(funcs)
var err error
tmpl, err = tmpl.Parse(tpl)
if err != nil {
log.Fatal(err)
}
for _, t := range []string{
bytesTpl,
intsTpl,
} {
var err error
tmpl, err = tmpl.Parse(t)
if err != nil {
log.Fatal(err)
}
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, pb)
if err != nil {
log.Fatal(err)
}
gocode, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
}
f, err := os.Create(*outPth)
if err != nil {
log.Fatal(err)
}
_, err = f.Write(gocode)
if err != nil {
log.Fatal(err)
}
f.Close()
}
type bitback struct {
Package string
Max int
}
type byt struct {
I int
Or string
And int
Shift int
Dir string
}
var (
funcs = template.FuncMap{
"pack": func(width int) [][]byt {
bs := [][]byt{{}}
var x int
and := 1<<uint(width) - 1
for i := 0; i < 8; i++ {
shift := (i * width) % 8
if shift+width > 8 {
a1 := 7 >> uint(width-(8-shift))
a2 := 7 - a1
s2 := 8 - shift
bs[x] = append(bs[x],
byt{
I: i,
And: a1,
Shift: shift,
Dir: "<<",
})
x++
bs = append(bs, []byt{})
bs[x] = append(bs[x],
byt{
I: i,
And: a2,
Or: " |\n",
Shift: s2,
Dir: ">>",
},
)
} else {
o := " |\n"
if shift+width == 8 {
o = ""
}
bs[x] = append(bs[x], byt{
I: i,
And: and,
Or: o,
Shift: shift,
Dir: "<<",
})
if shift+width == 8 && i < 7 {
bs = append(bs, []byt{})
x++
}
}
}
return bs
},
"uint8": func(width, i int) string {
shift := (i * width) % 8
index := (i * width) / 8
mask := ((1 << uint(width)) - 1) << uint(shift)
if mask < (1 << 8) {
return fmt.Sprintf("(uint8(vals[%d] & %d) >> %d),", index, mask, shift)
}
return fmt.Sprintf(
"%s | %s,",
fmt.Sprintf("(uint8(vals[%d] & %d) >> %d)", index, mask&((1<<8)-1), shift),
fmt.Sprintf("(uint8(vals[%d] & %d) << %d)", index+1, mask>>8, 8-shift),
)
},
"N": func(start, end int) (stream chan int) {
stream = make(chan int)
go func() {
for i := start; i <= end; i++ {
stream <- i
}
close(stream)
}()
return
},
}
/*
*/
tpl = `package {{.Package}}
// Code generated by github.com/BillHeroInc/parquet. DO NOT EDIT.
const MaxSize = {{.Max}}
func Pack(b []byte, width int, vals []uint8) []byte {
switch width {
{{range $i := N 1 .Max }}case {{$i}}:
return pack{{$i}}(b, vals)
{{end}}default:
return b
}
}
{{range $i := N 1 .Max}}
func pack{{$i}}(b []byte, vals []uint8) []byte {
return append(b, {{template "bytes" $i}} )
}
{{end}}
func Unpack(width int, vals []byte) []uint8 {
switch width {
{{range $i := N 1 .Max }}case {{$i}}:
return unpack{{$i}}(vals)
{{end}}default:
return []uint8{}
}
}
{{range $i := N 1 .Max }}
func unpack{{$i}}(vals []byte) []uint8 { {{template "ints" .}}
}
{{end}}
`
bytesTpl = `{{define "bytes"}}
{{ $bytes := pack .}} {{range $byte := $bytes}} ( {{ range $b := $byte}} byte((vals[{{$b.I}}]&{{$b.And}}){{$b.Dir}}{{$b.Shift}}){{$b.Or}}{{end}} ),
{{end}}
{{end}}`
intsTpl = `{{define "ints"}}{{$width := .}}
return []uint8{
{{range $i := N 0 7}} {{uint8 $width $i}}
{{end}} }{{end}}`
)