forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_changeset.hh
162 lines (132 loc) · 4.33 KB
/
serial_changeset.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
#ifndef MASSTREE_SERIAL_CHANGESET_HH
#define MASSTREE_SERIAL_CHANGESET_HH
#include <iterator>
template <typename IDX>
class serial_changeset_iterator : public std::iterator<std::input_iterator_tag, serial_changeset_iterator<IDX> > {
public:
inline serial_changeset_iterator();
inline serial_changeset_iterator(const char* x);
inline bool check(serial_changeset_iterator<IDX> last) const;
inline IDX index() const;
inline lcdf::Str value() const;
inline int32_t value_length() const;
inline const serial_changeset_iterator<IDX>& operator*() const;
inline const serial_changeset_iterator<IDX>* operator->() const;
inline void operator++();
inline bool operator==(serial_changeset_iterator<IDX> x) const;
inline bool operator!=(serial_changeset_iterator<IDX> x) const;
private:
const char* p_;
};
template <typename IDX>
class serial_changeset {
public:
inline serial_changeset();
inline serial_changeset(lcdf::Str str);
inline bool empty() const;
inline bool single_index() const;
inline IDX last_index() const;
inline serial_changeset_iterator<IDX> begin() const;
inline serial_changeset_iterator<IDX> end() const;
private:
lcdf::Str str_;
};
template <typename IDX>
inline serial_changeset_iterator<IDX>::serial_changeset_iterator()
: p_() {
}
template <typename IDX>
inline serial_changeset_iterator<IDX>::serial_changeset_iterator(const char* x)
: p_(x) {
}
template <typename IDX>
inline const serial_changeset_iterator<IDX>& serial_changeset_iterator<IDX>::operator*() const {
return *this;
}
template <typename IDX>
inline const serial_changeset_iterator<IDX>* serial_changeset_iterator<IDX>::operator->() const {
return this;
}
template <typename IDX>
inline IDX serial_changeset_iterator<IDX>::index() const {
#if HAVE_INDIFFERENT_ALIGNMENT
return *reinterpret_cast<const IDX*>(p_);
#else
IDX idx;
memcpy(&idx, p_, sizeof(IDX));
return idx;
#endif
}
template <typename IDX>
inline int32_t serial_changeset_iterator<IDX>::value_length() const {
#if HAVE_INDIFFERENT_ALIGNMENT
return *reinterpret_cast<const int32_t*>(p_ + sizeof(IDX));
#else
int32_t len;
memcpy(&len, p_ + sizeof(IDX), sizeof(int32_t));
return len;
#endif
}
template <typename IDX>
inline lcdf::Str serial_changeset_iterator<IDX>::value() const {
return lcdf::Str(p_ + sizeof(IDX) + sizeof(int32_t), value_length());
}
template <typename IDX>
inline bool serial_changeset_iterator<IDX>::check(serial_changeset_iterator<IDX> last) const {
int32_t len;
return last.p_ - p_ >= ssize_t(sizeof(IDX) + sizeof(int32_t))
&& (len = value_length()) >= 0
&& last.p_ - (p_ + sizeof(IDX) + sizeof(int32_t)) >= len;
}
template <typename IDX>
inline void serial_changeset_iterator<IDX>::operator++() {
p_ += sizeof(IDX) + sizeof(int32_t) + value_length();
}
template <typename IDX>
inline bool serial_changeset_iterator<IDX>::operator==(serial_changeset_iterator<IDX> x) const {
return p_ == x.p_;
}
template <typename IDX>
inline bool serial_changeset_iterator<IDX>::operator!=(serial_changeset_iterator<IDX> x) const {
return p_ != x.p_;
}
template <typename IDX>
inline serial_changeset<IDX>::serial_changeset()
: str_() {
}
template <typename IDX>
inline serial_changeset<IDX>::serial_changeset(lcdf::Str str)
: str_(str) {
serial_changeset_iterator<IDX> ib(str_.begin()), ie(str_.end());
while (ib != ie && ib.check(ie))
++ib;
if (ib != ie)
str_ = lcdf::Str();
}
template <typename IDX>
inline bool serial_changeset<IDX>::empty() const {
return str_.empty();
}
template <typename IDX>
inline bool serial_changeset<IDX>::single_index() const {
return !str_.empty()
&& (serial_changeset_iterator<IDX>(str_.begin()).value_length()
== str_.length() - int(sizeof(IDX) + sizeof(int32_t)));
}
template <typename IDX>
inline IDX serial_changeset<IDX>::last_index() const {
IDX idx = IDX(-1);
auto endit = end();
for (auto it = begin(); it != endit; ++it)
idx = it.index();
return idx;
}
template <typename IDX>
inline serial_changeset_iterator<IDX> serial_changeset<IDX>::begin() const {
return serial_changeset_iterator<IDX>(str_.begin());
}
template <typename IDX>
inline serial_changeset_iterator<IDX> serial_changeset<IDX>::end() const {
return serial_changeset_iterator<IDX>(str_.end());
}
#endif