Debian Codegraph @

varint-GB.go

raw symbols SCIP
1 // Copyright © 2018-2021 Wei Shen <[email protected]>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package unik
22
23 var offsets = []uint8{56, 48, 40, 32, 24, 16, 8, 0}
24
25 // PutUint64s endcodes two uint64s into 2-16 bytes, and returns control byte
26 // and encoded byte length.
27 func PutUint64s(buf []byte, v1, v2 uint64) (ctrl byte, n int) {
28 blen := byteLength(v1)
29 ctrl |= byte(blen - 1)
30 for _, offset := range offsets[8-blen:] {
31 buf[n] = byte((v1 >> offset) & 0xff)
32 n++
33 }
34
35 ctrl <<= 3
36 blen = byteLength(v2)
37 ctrl |= byte(blen - 1)
38 for _, offset := range offsets[8-blen:] {
39 buf[n] = byte((v2 >> offset) & 0xff)
40 n++
41 }
42 return
43 }
44
45 // Uint64s decode from encoded bytes
46 func Uint64s(ctrl byte, buf []byte) (values [2]uint64, n int) {
47 blens := ctrlByte2ByteLengths[ctrl]
48 if len(buf) < int(blens[0]+blens[1]) {
49 return values, 0
50 }
51 for i := 0; i < 2; i++ {
52 for j := uint8(0); j < blens[i]; j++ {
53 values[i] <<= 8
54 values[i] |= uint64(buf[n])
55 n++
56 }
57 }
58
59 return
60 }
61
62 func byteLength(n uint64) uint8 {
63 if n < 256 {
64 return 1
65 }
66 if n < 65536 {
67 return 2
68 }
69 if n < 16777216 {
70 return 3
71 }
72 if n < 4294967296 {
73 return 4
74 }
75 if n < 1099511627776 {
76 return 5
77 }
78 if n < 281474976710656 {
79 return 6
80 }
81 if n < 72057594037927936 {
82 return 7
83 }
84 return 8
85 }
86
87 var ctrlByte2ByteLengths = [64][2]uint8{
88 {1, 1}, // 0, 0b000000
89 {1, 2},
90 {1, 3},
91 {1, 4},
92 {1, 5},
93 {1, 6},
94 {1, 7},
95 {1, 8},
96 {2, 1}, // 8, 0b001000
97 {2, 2},
98 {2, 3},
99 {2, 4},
100 {2, 5},
101 {2, 6},
102 {2, 7},
103 {2, 8},
104 {3, 1}, // 16, 0b010000
105 {3, 2},
106 {3, 3},
107 {3, 4},
108 {3, 5},
109 {3, 6},
110 {3, 7},
111 {3, 8},
112 {4, 1}, // 24, 0b011000
113 {4, 2},
114 {4, 3},
115 {4, 4},
116 {4, 5},
117 {4, 6},
118 {4, 7},
119 {4, 8},
120 {5, 1}, // 32, 0b100000
121 {5, 2},
122 {5, 3},
123 {5, 4},
124 {5, 5},
125 {5, 6},
126 {5, 7},
127 {5, 8},
128 {6, 1}, // 40, 0b101000
129 {6, 2},
130 {6, 3},
131 {6, 4},
132 {6, 5},
133 {6, 6},
134 {6, 7},
135 {6, 8},
136 {7, 1}, // 48, 0b110000
137 {7, 2},
138 {7, 3},
139 {7, 4},
140 {7, 5},
141 {7, 6},
142 {7, 7},
143 {7, 8},
144 {8, 1}, // 56, 0b111000
145 {8, 2},
146 {8, 3},
147 {8, 4},
148 {8, 5},
149 {8, 6},
150 {8, 7},
151 {8, 8},
152 }