-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt_utils.go
61 lines (53 loc) · 1.62 KB
/
bolt_utils.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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package index
import (
"encoding/binary"
"image"
"math"
)
func pointToBytes(point image.Point) []byte {
bytes := make([]byte, binary.MaxVarintLen64*2)
xSize := binary.PutVarint(bytes, int64(point.X))
ySize := binary.PutVarint(bytes[xSize:], int64(point.Y))
return bytes[:xSize+ySize]
}
func bytesToPoint(bytes []byte) image.Point {
x, xSize := binary.Varint(bytes)
y, _ := binary.Varint(bytes[xSize:])
return image.Point{X: int(x), Y: int(y)}
}
func intToBytes(i int) []byte {
bytes := make([]byte, binary.MaxVarintLen64)
size := binary.PutVarint(bytes, int64(i))
return bytes[:size]
}
func bytesToInt(bytes []byte) int {
i, _ := binary.Varint(bytes)
return int(i)
}
func floatsToBytes(floats []float64) []byte {
buf := make([]byte, len(floats)*8)
for i, f := range floats {
binary.BigEndian.PutUint64(buf[i*8:], math.Float64bits(f))
}
return buf
}
func bytesToFloats(in []byte) []float64 {
floats := make([]float64, len(in)/8)
for i := range floats {
floats[i] = math.Float64frombits(binary.BigEndian.Uint64(in[i*8:]))
}
return floats
}