-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.go
187 lines (157 loc) · 4.03 KB
/
binary.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
package yog
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
)
func decodeChunk(input []byte) ([]uint32, []uint32, error) {
var distances = make([]uint32, len(input)/8)
var durations = make([]uint32, len(input)/8)
for i := 0; i < len(input); i += 8 {
buffer0 := bytes.NewBuffer(input[i : i+4])
buffer1 := bytes.NewBuffer(input[i+4 : i+8])
err := binary.Read(buffer0, binary.LittleEndian, &durations[i/8])
if err != nil {
return nil, nil, err
}
err = binary.Read(buffer1, binary.LittleEndian, &distances[i/8])
if err != nil {
return nil, nil, err
}
}
return durations, distances, nil
}
func encodeChunk(durations, distances []uint32) ([]byte, error) {
var binBuf bytes.Buffer
for i := range durations {
err := binary.Write(&binBuf, binary.LittleEndian, durations[i])
if err != nil {
return nil, err
}
err = binary.Write(&binBuf, binary.LittleEndian, distances[i])
if err != nil {
return nil, err
}
}
return binBuf.Bytes(), nil
}
func decode(input []byte) (uint32, uint32, error) {
buffer0 := bytes.NewBuffer(input[0:4])
buffer1 := bytes.NewBuffer(input[4:8])
var duration, distance uint32
err := binary.Read(buffer0, binary.LittleEndian, &duration)
if err != nil {
return 0, 0, err
}
err = binary.Read(buffer1, binary.LittleEndian, &distance)
if err != nil {
return 0, 0, err
}
return duration, distance, nil
}
func encode(duration, distance uint32) ([]byte, error) {
var binBuf bytes.Buffer
err := binary.Write(&binBuf, binary.LittleEndian, duration)
if err != nil {
return nil, err
}
err = binary.Write(&binBuf, binary.LittleEndian, distance)
if err != nil {
return nil, err
}
return binBuf.Bytes(), nil
}
// Encode MatrixInfo Result -> binary
// i = o * d.length + d
func EncodeNumber(data string) ([]byte, error) {
var resp MatrixData
err := json.Unmarshal([]byte(data), &resp)
if err != nil {
return nil, err
}
result := ""
for _, row := range resp.Rows {
rowStr := "|"
for _, e := range row.Elements {
rowStr = preByteConcat(rowStr, fmt.Sprintf("%v,%v|", e.Duration.Value, e.Distance.Value))
}
result = preByteConcat(result, rowStr)
result = preByteConcat(result, "\n")
}
return []byte(result), nil
}
func preByteConcat(str1, str2 string) string {
buf := []byte(str1)
buf = append(buf, str2...)
return string(buf)
}
// Encode MatrixInfo Result -> binary
// i = o * d.length + d
func Encode(data string) ([]byte, error) {
var resp MatrixData
err := json.Unmarshal([]byte(data), &resp)
if err != nil {
return nil, err
}
header, err := encode(uint32(len(resp.Rows)), uint32(len(resp.Rows[0].Elements)))
if err != nil {
return nil, err
}
// 将 MatrixData.Rows 转化成 binary
res := make([]byte, 0)
// add header
res = append(res, header...)
// source index
for _, row := range resp.Rows {
// destination index
for _, element := range row.Elements {
chunk, err := encode(element.Duration.Value, element.Distance.Value)
if err != nil {
return nil, err
}
res = append(res, chunk...)
}
}
return res, nil
}
// Decode binary -> MatrixInfo Result
func Decode(bin []byte) (string, error) {
// check data
if len(bin)%8 > 0 {
return "", errors.New("illegal binary data format")
}
//read header
sourceLength, destinationLength, err := decode(bin[0:8])
if err != nil {
return "", errors.New("binary header decode failed")
}
bin = bin[8:]
// resize result
var m MatrixData
m.Rows = make([]MatrixRow, sourceLength)
for i := range m.Rows {
m.Rows[i].Elements = make([]MatrixElement, destinationLength)
}
// decode
for i := 0; i < len(bin); i = i + 8 {
duration, distance, err := decode(bin[i : i+8])
if err != nil {
return "", errors.New("binary decode failed")
}
n := uint32(i / 8)
var d = n % destinationLength
var s = (n - d) / destinationLength
//fmt.Println(fmt.Sprintf("s=%v, d=%v", s, d))
m.Rows[s].Elements[d] = MatrixElement{
Distance: Value{distance},
Duration: Value{duration},
}
}
marshal, err := json.Marshal(m)
if err != nil {
return "", err
}
return string(marshal), nil
}