-
Notifications
You must be signed in to change notification settings - Fork 2
/
varstr.h
101 lines (80 loc) · 2.73 KB
/
varstr.h
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
#pragma once
#include "dbcore/sm-common.h"
#include "util.h"
#ifdef MASSTREE
#include "masstree/string_slice.hh"
#endif
namespace ermia {
struct varstr {
friend std::ostream &operator<<(std::ostream &o, const varstr &k);
public:
inline varstr() : l(0), p(NULL) {}
inline varstr(const uint8_t *p, uint32_t l) : l(l), p(p) {}
inline varstr(const char *p, uint32_t l) : l(l), p((const uint8_t *)p) {}
inline void copy_from(const uint8_t *s, uint32_t l) {
copy_from((const char *)s, l);
}
inline void copy_from(const varstr *v) { copy_from(v->p, v->l); }
inline void copy_from(const char *s, uint32_t ll) {
if (ll) {
memcpy((void *)p, s, ll);
}
l = ll;
}
inline bool operator==(const varstr &that) const {
if (size() != that.size()) return false;
return memcmp(data(), that.data(), size()) == 0;
}
inline bool operator!=(const varstr &that) const { return !operator==(that); }
inline bool operator<(const varstr &that) const {
int r = memcmp(data(), that.data(), std::min(size(), that.size()));
return r < 0 || (r == 0 && size() < that.size());
}
inline bool operator>=(const varstr &that) const { return !operator<(that); }
inline bool operator<=(const varstr &that) const {
int r = memcmp(data(), that.data(), std::min(size(), that.size()));
return r < 0 || (r == 0 && size() <= that.size());
}
inline bool operator>(const varstr &that) const { return !operator<=(that); }
inline int compare(const varstr &that) const {
return memcmp(data(), that.data(), std::min(size(), that.size()));
}
inline uint64_t slice() const {
uint64_t ret = 0;
uint8_t *rp = (uint8_t *)&ret;
for (uint32_t i = 0; i < std::min(l, uint64_t(8)); i++) rp[i] = p[i];
return util::host_endian_trfm<uint64_t>()(ret);
}
#ifdef MASSTREE
inline uint64_t slice_at(int pos) const {
return string_slice<uint64_t>::make_comparable((const char *)p + pos,
std::min(int(l - pos), 8));
}
#endif
inline varstr shift() const {
ASSERT(l >= 8);
return varstr(p + 8, l - 8);
}
inline varstr shift_many(uint32_t n) const {
ASSERT(l >= 8 * n);
return varstr(p + 8 * n, l - 8 * n);
}
inline uint32_t size() const { return l; }
inline const uint8_t *data() const { return p; }
inline uint8_t *data() { return (uint8_t *)(void *)p; }
inline bool empty() const { return not size(); }
#ifdef MASSTREE
inline operator lcdf::Str() const { return lcdf::Str(p, l); }
#endif
inline void prefetch() {
uint32_t i = 0;
do {
::prefetch((const char *)(p + i));
i += CACHE_LINE_SIZE;
} while (i < l);
}
uint64_t l;
fat_ptr ptr;
const uint8_t *p; // must be the last field
};
} // namespace ermia