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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#pragma once
#include "../base/assert.hpp"
#include "../base/base.hpp"
#include "../std/string.hpp"
#include "../std/sstream.hpp"
#include "../std/utility.hpp"
namespace m2
{
template <int kDepthLevels = 31> class CellId
{
public:
// TODO: Move CellId::DEPTH_LEVELS to private.
static int const DEPTH_LEVELS = kDepthLevels;
static uint32_t const MAX_COORD = 1U << DEPTH_LEVELS;
CellId() : m_Bits(0), m_Level(0)
{
ASSERT(IsValid(), ());
}
explicit CellId(string const & s)
{
*this = FromString(s);
}
static CellId Root()
{
return CellId(0, 0);
}
static CellId FromBitsAndLevel(uint64_t bits, int level)
{
return CellId(bits, level);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Simple getters
int Level() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
return m_Level;
}
CellId Parent() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT_GREATER(m_Level, 0, ());
return CellId(m_Bits >> 2, m_Level - 1);
}
CellId AncestorAtLevel(int level) const
{
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT_GREATER_OR_EQUAL(m_Level, level, ());
return CellId(m_Bits >> ((m_Level - level) << 1), level);
}
CellId Child(int8_t c) const
{
ASSERT(c >= 0 && c < 4, (c, m_Bits, m_Level));
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT_LESS(m_Level, DEPTH_LEVELS - 1, ());
return CellId((m_Bits << 2) | c, m_Level + 1);
}
char WhichChildOfParent() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT_GREATER(m_Level, 0, ());
return m_Bits & 3;
}
uint64_t SubTreeSize(int depth) const
{
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT(m_Level < depth && depth <= DEPTH_LEVELS, (m_Bits, m_Level, depth));
return TreeSizeForDepth(depth - m_Level);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Operators
bool operator == (CellId const & cellId) const
{
ASSERT(IsValid(), (m_Bits, m_Level));
ASSERT(cellId.IsValid(), (cellId.m_Bits, cellId.m_Level));
return m_Bits == cellId.m_Bits && m_Level == cellId.m_Level;
}
bool operator != (CellId const & cellId) const
{
return !(*this == cellId);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Conversion to/from string
string ToString() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
string result(m_Level, '0');
uint64_t bits = m_Bits;
for (int i = 0; i < m_Level; ++i, bits >>= 2)
result[m_Level - 1 - i] += (bits & 3);
ASSERT_EQUAL(*this, FromString(result), (m_Bits, m_Level, result));
return result;
}
// Is string @s a valid CellId representation?
// Note that empty string is a valid CellId.
static bool IsCellId(string const & s)
{
size_t const length = s.size();
if (length >= DEPTH_LEVELS)
return false;
for (size_t i = 0; i < length; ++i)
{
if (s[i] < '0' || s[i] > '3')
return false;
}
return true;
}
static CellId FromString(string const & s)
{
ASSERT(IsCellId(s), (s));
uint64_t bits = 0;
size_t const level = s.size();
ASSERT_LESS(level, static_cast<size_t>(DEPTH_LEVELS), (s));
for (size_t i = 0; i < level; ++i)
{
ASSERT((s[i] >= '0') && (s[i] <= '3'), (s, i));
bits = (bits << 2) | (s[i] - '0');
}
return CellId(bits, static_cast<int>(level));
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Conversion to/from point
// Cell area width and height.
// Should be 1 for the bottom level cell.
uint32_t Radius() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
return 1 << (DEPTH_LEVELS - 1 - m_Level);
}
pair<uint32_t, uint32_t> XY() const
{
ASSERT(IsValid(), (m_Bits, m_Level));
uint32_t offset = 1 << (DEPTH_LEVELS - 1 - m_Level);
pair<uint32_t, uint32_t> xy(offset, offset);
uint64_t bits = m_Bits;
while (bits > 0)
{
offset <<= 1;
if (bits & 1)
xy.first += offset;
if (bits & 2)
xy.second += offset;
bits >>= 2;
}
ASSERT_EQUAL(*this, FromXY(xy.first, xy.second, m_Level), ());
return xy;
}
static CellId FromXY(uint32_t x, uint32_t y, int level)
{
ASSERT_LESS(level, static_cast<int>(DEPTH_LEVELS), (x, y, level));
// Since MAX_COORD == 1 << DEPTH_LEVELS, if x|y == MAX_COORD, they should be decremented.
if (x >= MAX_COORD)
{
ASSERT_EQUAL(x, static_cast<uint32_t>(MAX_COORD), (x, y, level));
x = MAX_COORD - 1;
}
if (y >= MAX_COORD)
{
ASSERT_EQUAL(y, static_cast<uint32_t>(MAX_COORD), (x, y, level));
y = MAX_COORD - 1;
}
x >>= DEPTH_LEVELS - level;
y >>= DEPTH_LEVELS - level;
// This operation is called "perfect shuffle". Optimized bit trick can be used here.
uint64_t bits = 0;
for (int i = 0; i < level; ++i)
bits |= ((uint64_t((y >> i) & 1) << (2 * i + 1)) | (uint64_t((x >> i) & 1) << (2 * i)));
return CellId(bits, level);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Ordering
struct LessLevelOrder
{
bool operator() (CellId<DEPTH_LEVELS> const & id1, CellId<DEPTH_LEVELS> const & id2) const
{
if (id1.m_Level != id2.m_Level)
return id1.m_Level < id2.m_Level;
if (id1.m_Bits != id2.m_Bits)
return id1.m_Bits < id2.m_Bits;
return false;
}
};
struct LessPreOrder
{
bool operator() (CellId<DEPTH_LEVELS> const & id1, CellId<DEPTH_LEVELS> const & id2) const
{
int64_t const n1 = id1.ToInt64LevelZOrder(DEPTH_LEVELS);
int64_t const n2 = id2.ToInt64LevelZOrder(DEPTH_LEVELS);
ASSERT_EQUAL(n1 < n2, id1.ToString() < id2.ToString(), (id1, id2));
return n1 < n2;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Numbering
// Default ToInt64().
int64_t ToInt64(int depth) const
{
return ToInt64LevelZOrder(depth);
}
// Default FromInt64().
static CellId FromInt64(int64_t v, int depth)
{
return FromInt64LevelZOrder(v, depth);
}
// Level order, numbering by Z-curve.
int64_t ToInt64LevelZOrder(int depth) const
{
ASSERT(0 < depth && depth <= DEPTH_LEVELS, (m_Bits, m_Level, depth));
ASSERT(IsValid(), (m_Bits, m_Level));
if (m_Level >= depth)
return AncestorAtLevel(depth - 1).ToInt64(depth);
else
{
uint64_t bits = m_Bits, res = 0;
for (int i = 0; i <= m_Level; ++i, bits >>= 2)
res += bits + 1;
bits = m_Bits;
for (int i = m_Level + 1; i < depth; ++i)
{
bits <<= 2;
res += bits;
}
ASSERT_GREATER(res, 0, (m_Bits, m_Level));
ASSERT_LESS_OR_EQUAL(res, TreeSizeForDepth(depth), (m_Bits, m_Level));
return static_cast<int64_t>(res);
}
}
// Level order, numbering by Z-curve.
static CellId FromInt64LevelZOrder(int64_t v, int depth)
{
ASSERT_GREATER(v, 0, ());
ASSERT(0 < depth && depth <= DEPTH_LEVELS, (v, depth));
ASSERT_LESS_OR_EQUAL(v, TreeSizeForDepth(depth), ());
uint64_t bits = 0;
int level = 0;
--v;
while (v > 0)
{
bits <<= 2;
++level;
uint64_t subTreeSize = TreeSizeForDepth(depth - level);
for (--v; v >= subTreeSize; v -= subTreeSize)
++bits;
}
return CellId(bits, level);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
private:
static uint64_t TreeSizeForDepth(int depth)
{
ASSERT(0 < depth && depth <= DEPTH_LEVELS, (depth));
return ((1ULL << 2 * depth) - 1) / 3ULL;
}
CellId(uint64_t bits, int level) : m_Bits(bits), m_Level(level)
{
ASSERT_LESS(level, static_cast<uint32_t>(DEPTH_LEVELS), (bits, level));
ASSERT_LESS(bits, 1ULL << m_Level * 2, (bits, m_Level));
ASSERT(IsValid(), (m_Bits, m_Level));
}
bool IsValid() const
{
if (m_Level < 0 || m_Level >= DEPTH_LEVELS)
return false;
if (m_Bits >= (1ULL << m_Level * 2))
return false;
return true;
}
uint64_t m_Bits;
int m_Level;
};
template <int DEPTH_LEVELS> string debug_print(CellId<DEPTH_LEVELS> const & id)
{
ostringstream out;
out << "CellId<" << DEPTH_LEVELS << ">(\"" << id.ToString().c_str() << "\")";
return out.str();
}
}
|