forked from suyash/ulid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulid_uint128.hh
539 lines (425 loc) · 13.7 KB
/
ulid_uint128.hh
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#pragma once
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <random>
#include <vector>
namespace ulid {
/**
* ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
* */
typedef __uint128_t ULID;
/**
* EncodeTime will encode the first 6 bytes of a uint8_t array to the passed
* timestamp
* */
void EncodeTime(time_t timestamp, ULID& ulid) {
ULID t = static_cast<uint8_t>(timestamp >> 40);
t <<= 8;
t |= static_cast<uint8_t>(timestamp >> 32);
t <<= 8;
t |= static_cast<uint8_t>(timestamp >> 24);
t <<= 8;
t |= static_cast<uint8_t>(timestamp >> 16);
t <<= 8;
t |= static_cast<uint8_t>(timestamp >> 8);
t <<= 8;
t |= static_cast<uint8_t>(timestamp);
t <<= 80;
ULID mask = 1;
mask <<= 80;
mask--;
ulid = t | (ulid & mask);
}
/**
* EncodeTimeNow will encode a ULID using the time obtained using std::time(nullptr)
* */
void EncodeTimeNow(ULID& ulid) {
EncodeTime(std::time(nullptr), ulid);
}
/**
* EncodeTimeSystemClockNow will encode a ULID using the time obtained using
* std::chrono::system_clock::now() by taking the timestamp in milliseconds.
* */
void EncodeTimeSystemClockNow(ULID& ulid) {
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
EncodeTime(ms.count(), ulid);
}
/**
* EncodeEntropy will encode the last 10 bytes of the passed uint8_t array with
* the values generated using the passed random number generator.
* */
void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid) {
ulid = (ulid >> 80) << 80;
ULID e = rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
e <<= 8;
e |= rng();
ulid |= e;
}
/**
* EncodeEntropyRand will encode a ulid using std::rand
*
* std::rand returns values in [0, RAND_MAX]
* */
void EncodeEntropyRand(ULID& ulid) {
ulid = (ulid >> 80) << 80;
ULID e = (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
e <<= 8;
e |= (std::rand() * 255ull) / RAND_MAX;
ulid |= e;
}
std::uniform_int_distribution<uint8_t> Distribution_0_255(0, 255);
/**
* EncodeEntropyMt19937 will encode a ulid using std::mt19937
*
* It also creates a std::uniform_int_distribution to generate values in [0, 255]
* */
void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid) {
ulid = (ulid >> 80) << 80;
ULID e = Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
e <<= 8;
e |= Distribution_0_255(generator);
ulid |= e;
}
/**
* Encode will create an encoded ULID with a timestamp and a generator.
* */
void Encode(time_t timestamp, const std::function<uint8_t()>& rng, ULID& ulid) {
EncodeTime(timestamp, ulid);
EncodeEntropy(rng, ulid);
}
/**
* EncodeNowRand = EncodeTimeNow + EncodeEntropyRand.
* */
void EncodeNowRand(ULID& ulid) {
EncodeTimeNow(ulid);
EncodeEntropyRand(ulid);
}
/**
* Create will create a ULID with a timestamp and a generator.
* */
ULID Create(time_t timestamp, const std::function<uint8_t()>& rng) {
ULID ulid = 0;
Encode(timestamp, rng, ulid);
return ulid;
}
/**
* CreateNowRand:EncodeNowRand = Create:Encode.
* */
ULID CreateNowRand() {
ULID ulid = 0;
EncodeNowRand(ulid);
return ulid;
}
/**
* Crockford's Base32
* */
const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
/**
* MarshalTo will marshal a ULID to the passed character array.
*
* Implementation taken directly from oklog/ulid
* (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
*
* timestamp:
* dst[0]: first 3 bits of data[0]
* dst[1]: last 5 bits of data[0]
* dst[2]: first 5 bits of data[1]
* dst[3]: last 3 bits of data[1] + first 2 bits of data[2]
* dst[4]: bits 3-7 of data[2]
* dst[5]: last bit of data[2] + first 4 bits of data[3]
* dst[6]: last 4 bits of data[3] + first bit of data[4]
* dst[7]: bits 2-6 of data[4]
* dst[8]: last 2 bits of data[4] + first 3 bits of data[5]
* dst[9]: last 5 bits of data[5]
*
* entropy:
* follows similarly, except now all components are set to 5 bits.
* */
void MarshalTo(const ULID& ulid, char dst[26]) {
// 10 byte timestamp
dst[0] = Encoding[(static_cast<uint8_t>(ulid >> 120) & 224) >> 5];
dst[1] = Encoding[static_cast<uint8_t>(ulid >> 120) & 31];
dst[2] = Encoding[(static_cast<uint8_t>(ulid >> 112) & 248) >> 3];
dst[3] = Encoding[((static_cast<uint8_t>(ulid >> 112) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 104) & 192) >> 6)];
dst[4] = Encoding[(static_cast<uint8_t>(ulid >> 104) & 62) >> 1];
dst[5] = Encoding[((static_cast<uint8_t>(ulid >> 104) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 96) & 240) >> 4)];
dst[6] = Encoding[((static_cast<uint8_t>(ulid >> 96) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 88) & 128) >> 7)];
dst[7] = Encoding[(static_cast<uint8_t>(ulid >> 88) & 124) >> 2];
dst[8] = Encoding[((static_cast<uint8_t>(ulid >> 88) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 80) & 224) >> 5)];
dst[9] = Encoding[static_cast<uint8_t>(ulid >> 80) & 31];
// 16 bytes of entropy
dst[10] = Encoding[(static_cast<uint8_t>(ulid >> 72) & 248) >> 3];
dst[11] = Encoding[((static_cast<uint8_t>(ulid >> 72) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 64) & 192) >> 6)];
dst[12] = Encoding[(static_cast<uint8_t>(ulid >> 64) & 62) >> 1];
dst[13] = Encoding[((static_cast<uint8_t>(ulid >> 64) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 56) & 240) >> 4)];
dst[14] = Encoding[((static_cast<uint8_t>(ulid >> 56) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 48) & 128) >> 7)];
dst[15] = Encoding[(static_cast<uint8_t>(ulid >> 48) & 124) >> 2];
dst[16] = Encoding[((static_cast<uint8_t>(ulid >> 48) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 40) & 224) >> 5)];
dst[17] = Encoding[static_cast<uint8_t>(ulid >> 40) & 31];
dst[18] = Encoding[(static_cast<uint8_t>(ulid >> 32) & 248) >> 3];
dst[19] = Encoding[((static_cast<uint8_t>(ulid >> 32) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 24) & 192) >> 6)];
dst[20] = Encoding[(static_cast<uint8_t>(ulid >> 24) & 62) >> 1];
dst[21] = Encoding[((static_cast<uint8_t>(ulid >> 24) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 16) & 240) >> 4)];
dst[22] = Encoding[((static_cast<uint8_t>(ulid >> 16) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 8) & 128) >> 7)];
dst[23] = Encoding[(static_cast<uint8_t>(ulid >> 8) & 124) >> 2];
dst[24] = Encoding[((static_cast<uint8_t>(ulid >> 8) & 3) << 3) | (((static_cast<uint8_t>(ulid)) & 224) >> 5)];
dst[25] = Encoding[(static_cast<uint8_t>(ulid)) & 31];
}
/**
* Marshal will marshal a ULID to a std::string.
* */
std::string Marshal(const ULID& ulid) {
char data[27];
data[26] = '\0';
MarshalTo(ulid, data);
return std::string(data);
}
/**
* MarshalBinaryTo will Marshal a ULID to the passed byte array
* */
void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16]) {
// timestamp
dst[0] = static_cast<uint8_t>(ulid >> 120);
dst[1] = static_cast<uint8_t>(ulid >> 112);
dst[2] = static_cast<uint8_t>(ulid >> 104);
dst[3] = static_cast<uint8_t>(ulid >> 96);
dst[4] = static_cast<uint8_t>(ulid >> 88);
dst[5] = static_cast<uint8_t>(ulid >> 80);
// entropy
dst[6] = static_cast<uint8_t>(ulid >> 72);
dst[7] = static_cast<uint8_t>(ulid >> 64);
dst[8] = static_cast<uint8_t>(ulid >> 56);
dst[9] = static_cast<uint8_t>(ulid >> 48);
dst[10] = static_cast<uint8_t>(ulid >> 40);
dst[11] = static_cast<uint8_t>(ulid >> 32);
dst[12] = static_cast<uint8_t>(ulid >> 24);
dst[13] = static_cast<uint8_t>(ulid >> 16);
dst[14] = static_cast<uint8_t>(ulid >> 8);
dst[15] = static_cast<uint8_t>(ulid);
}
/**
* MarshalBinary will Marshal a ULID to a byte vector.
* */
std::vector<uint8_t> MarshalBinary(const ULID& ulid) {
std::vector<uint8_t> dst(16);
MarshalBinaryTo(ulid, dst.data());
return dst;
}
/**
* dec storesdecimal encodings for characters.
* 0xFF indicates invalid character.
* 48-57 are digits.
* 65-90 are capital alphabets.
* */
const uint8_t dec[256] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0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 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
/**
* UnmarshalFrom will unmarshal a ULID from the passed character array.
* */
void UnmarshalFrom(const char str[26], ULID& ulid) {
// timestamp
ulid = (dec[int(str[0])] << 5) | dec[int(str[1])];
ulid <<= 8;
ulid |= (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
ulid <<= 8;
ulid |= (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
ulid <<= 8;
ulid |= (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
ulid <<= 8;
ulid |= (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
ulid <<= 8;
ulid |= (dec[int(str[8])] << 5) | dec[int(str[9])];
// entropy
ulid <<= 8;
ulid |= (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
ulid <<= 8;
ulid |= (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
ulid <<= 8;
ulid |= (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
ulid <<= 8;
ulid |= (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
ulid <<= 8;
ulid |= (dec[int(str[16])] << 5) | dec[int(str[17])];
ulid <<= 8;
ulid |= (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
ulid <<= 8;
ulid |= (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
ulid <<= 8;
ulid |= (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
ulid <<= 8;
ulid |= (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
ulid <<= 8;
ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
}
/**
* Unmarshal will create a new ULID by unmarshaling the passed string.
* */
ULID Unmarshal(const std::string& str) {
ULID ulid;
UnmarshalFrom(str.c_str(), ulid);
return ulid;
}
/**
* UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
* */
void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid) {
// timestamp
ulid = b[0];
ulid <<= 8;
ulid |= b[1];
ulid <<= 8;
ulid |= b[2];
ulid <<= 8;
ulid |= b[3];
ulid <<= 8;
ulid |= b[4];
ulid <<= 8;
ulid |= b[5];
// entropy
ulid <<= 8;
ulid |= b[6];
ulid <<= 8;
ulid |= b[7];
ulid <<= 8;
ulid |= b[8];
ulid <<= 8;
ulid |= b[9];
ulid <<= 8;
ulid |= b[10];
ulid <<= 8;
ulid |= b[11];
ulid <<= 8;
ulid |= b[12];
ulid <<= 8;
ulid |= b[13];
ulid <<= 8;
ulid |= b[14];
ulid <<= 8;
ulid |= b[15];
}
/**
* Unmarshal will create a new ULID by unmarshaling the passed byte vector.
* */
ULID UnmarshalBinary(const std::vector<uint8_t>& b) {
ULID ulid;
UnmarshalBinaryFrom(b.data(), ulid);
return ulid;
}
/**
* CompareULIDs will compare two ULIDs.
* returns:
* -1 if ulid1 is Lexicographically before ulid2
* 1 if ulid1 is Lexicographically after ulid2
* 0 if ulid1 is same as ulid2
* */
int CompareULIDs(const ULID& ulid1, const ULID& ulid2) {
return -2 * (ulid1 < ulid2) - 1 * (ulid1 == ulid2) + 1;
}
/**
* Time will extract the timestamp used to generate a ULID
* */
time_t Time(const ULID& ulid) {
time_t ans = 0;
ans |= static_cast<uint8_t>(ulid >> 120);
ans <<= 8;
ans |= static_cast<uint8_t>(ulid >> 112);
ans <<= 8;
ans |= static_cast<uint8_t>(ulid >> 104);
ans <<= 8;
ans |= static_cast<uint8_t>(ulid >> 96);
ans <<= 8;
ans |= static_cast<uint8_t>(ulid >> 88);
ans <<= 8;
ans |= static_cast<uint8_t>(ulid >> 80);
return ans;
}
}; // namespace ulid