GeoDesk for C++
Fast and storage-efficient spatial database engine for OpenStreetMap features
Loading...
Searching...
No Matches
varint.h
Go to the documentation of this file.
1// Copyright (c) 2024 Clarisma / GeoDesk contributors
2// SPDX-License-Identifier: LGPL-3.0-only
3
4#pragma once
5
6#include <cassert>
7#include <cstdint>
8#include <string_view>
10
11namespace clarisma {
12
13inline uint64_t readVarint35(const uint8_t*& p)
14{
15 uint32_t val;
16 uint8_t b;
17 b = *p++;
18 val = b & 0x7f;
19 if ((b & 0x80) == 0) return val;
20 b = *p++;
21 val |= static_cast<uint32_t>(b & 0x7f) << 7;
22 if ((b & 0x80) == 0) return val;
23 b = *p++;
24 val |= static_cast<uint32_t>(b & 0x7f) << 14;
25 if ((b & 0x80) == 0) return val;
26 b = *p++;
27 val |= static_cast<uint32_t>(b & 0x7f) << 21;
28 if ((b & 0x80) == 0) return val;
29 b = *p++;
30 val |= static_cast<uint32_t>(b & 0x7f) << 28;
31 assert((b & 0x80) == 0);
32 return val;
33}
34
35inline uint32_t readVarint32(const uint8_t*& p)
36{
37 return static_cast<uint32_t>(readVarint35(p));
38}
39
40inline uint64_t readVarint64(const uint8_t*& p)
41{
42 uint64_t val;
43 uint8_t b;
44 b = *p++;
45 val = b & 0x7f;
46 if ((b & 0x80) == 0) return val;
47 b = *p++;
48 val |= static_cast<uint64_t>(b & 0x7f) << 7;
49 if ((b & 0x80) == 0) return val;
50 b = *p++;
51 val |= static_cast<uint64_t>(b & 0x7f) << 14;
52 if ((b & 0x80) == 0) return val;
53 b = *p++;
54 val |= static_cast<uint64_t>(b & 0x7f) << 21;
55 if ((b & 0x80) == 0) return val;
56 b = *p++;
57 val |= static_cast<uint64_t>(b & 0x7f) << 28;
58 if ((b & 0x80) == 0) return val;
59 b = *p++;
60 val |= static_cast<uint64_t>(b & 0x7f) << 35;
61 if ((b & 0x80) == 0) return val;
62 b = *p++;
63 val |= static_cast<uint64_t>(b & 0x7f) << 42;
64 if ((b & 0x80) == 0) return val;
65 b = *p++;
66 val |= static_cast<uint64_t>(b & 0x7f) << 49;
67 if ((b & 0x80) == 0) return val;
68 b = *p++;
69 val |= static_cast<uint64_t>(b & 0x7f) << 56;
70 if ((b & 0x80) == 0) return val;
71 b = *p++;
72 val |= static_cast<uint64_t>(b & 0x7f) << 63;
73 assert((b & 0x80) == 0);
74 return val;
75}
76
77
78inline int32_t readSignedVarint32(const uint8_t*& p)
79{
80 int64_t val = static_cast<int64_t>(readVarint35(p));
81 return static_cast<int32_t>((val >> 1) ^ -(val & 1));
82}
83
84inline int64_t readSignedVarint64(const uint8_t*& p)
85{
86 int64_t val = static_cast<int64_t>(readVarint64(p));
87 return (val >> 1) ^ -(val & 1);
88}
89
90
91inline std::string_view readStringView(const uint8_t*& p)
92{
93 uint32_t len = readVarint32(p);
94 std::string_view sv(reinterpret_cast<const char*>(p), len);
95 p += len;
96 return sv;
97}
98
99inline int countVarints(const void* pStart, const void* pEnd)
100{
101 int count = 0;
102 const char* p = reinterpret_cast<const char*>(pStart);
103 while (p < pEnd)
104 {
105 if (*p++ >= 0) count++;
106 }
107 return count;
108}
109
110inline void skipVarints(const uint8_t*& p, int count)
111{
112 do
113 {
114 uint8_t b = *p++;
115 count -= (b >> 7) ^ 1;
116 }
117 while (count);
118}
119
120
127inline void skipVarintsBackwardUnsafe(const uint8_t*& p, int count)
128{
129 do
130 {
131 p--;
132 uint8_t b = *(p-1);
133 count -= (b >> 7) ^ 1;
134 }
135 while (count);
136}
137
138
139inline void writeVarint(uint8_t*& p, uint64_t val)
140{
141 while (val >= 0x80)
142 {
143 *p++ = (val & 0x7f) | 0x80;
144 val >>= 7;
145 }
146 *p++ = static_cast<uint8_t>(val);
147}
148
149
150inline void writeSignedVarint(uint8_t*& p, int64_t val)
151{
152 writeVarint(p, (val << 1) ^ (val >> 63));
153}
154
160inline unsigned int varintSize(uint64_t v)
161{
162 return (64 - Bits::countLeadingZerosInNonZero64(v | 1) + 6) / 7;
163}
164
165
166inline uint64_t toZigzag(int64_t v)
167{
168 return (v << 1) ^ (v >> 63);
169}
170
171inline uint32_t toZigzag(int32_t v)
172{
173 return (v << 1) ^ (v >> 31);
174}
175
176inline int64_t fromZigzag(uint64_t v)
177{
178 return static_cast<int64_t>((v >> 1) ^ -static_cast<int64_t>(v & 1));
179}
180
181inline int32_t fromZigzag(uint32_t v)
182{
183 return static_cast<int32_t>((v >> 1) ^ -static_cast<int32_t>(v & 1));
184}
185} // namespace clarisma
Definition Arena.h:17
int countVarints(const void *pStart, const void *pEnd)
Definition varint.h:99
uint64_t readVarint35(const uint8_t *&p)
Definition varint.h:13
void skipVarints(const uint8_t *&p, int count)
Definition varint.h:110
std::string_view readStringView(const uint8_t *&p)
Definition varint.h:91
uint64_t toZigzag(int64_t v)
Definition varint.h:166
void writeSignedVarint(uint8_t *&p, int64_t val)
Definition varint.h:150
int64_t readSignedVarint64(const uint8_t *&p)
Definition varint.h:84
int32_t readSignedVarint32(const uint8_t *&p)
Definition varint.h:78
uint64_t readVarint64(const uint8_t *&p)
Definition varint.h:40
int64_t fromZigzag(uint64_t v)
Definition varint.h:176
unsigned int varintSize(uint64_t v)
Returns the number of bytes required to encode the given unsigned value as a varint (A varint require...
Definition varint.h:160
uint32_t readVarint32(const uint8_t *&p)
Definition varint.h:35
void skipVarintsBackwardUnsafe(const uint8_t *&p, int count)
Moves pointer backward, skipping over the specified numbers of varints.
Definition varint.h:127
void writeVarint(uint8_t *&p, uint64_t val)
Definition varint.h:139