-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorted_set.h
258 lines (241 loc) · 5.73 KB
/
sorted_set.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
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
#include "red_black_tree.h"
#include "size_balanced_tree.h"
#include <string>
#include <cassert>
#include <functional>
class redis_sorted_set
{
struct node_t
{
node_t *rb_parent, *rb_left, *rb_right;
node_t *sb_parent, *sb_left, *sb_right;
size_t rb_is_nil : 1;
size_t sb_is_nil : 1;
size_t rb_is_red : 1;
size_t sb_size : sizeof(size_t) * 8 - 3;
};
struct value_node_t : public node_t
{
template<class T>
value_node_t(T k, double s) : key(std::forward<T>(k)), score(s)
{
}
std::string key;
double score;
};
struct rb_interface
{
typedef std::string key_t;
typedef node_t node_t;
typedef value_node_t value_node_t;
static key_t const &get_key(value_node_t *node)
{
return node->key;
}
static bool is_nil(node_t *node)
{
return node->rb_is_nil;
}
static void set_nil(node_t *node, bool nil)
{
node->rb_is_nil = nil;
}
static node_t *get_parent(node_t *node)
{
return node->rb_parent;
}
static void set_parent(node_t *node, node_t *parent)
{
node->rb_parent = parent;
}
static node_t *get_left(node_t *node)
{
return node->rb_left;
}
static void set_left(node_t *node, node_t *left)
{
node->rb_left = left;
}
static node_t *get_right(node_t *node)
{
return node->rb_right;
}
static void set_right(node_t *node, node_t *right)
{
node->rb_right = right;
}
static bool is_black(node_t *node)
{
return !node->rb_is_red;
}
static void set_black(node_t *node, bool black)
{
node->rb_is_red = !black;
}
static bool predicate(key_t const &left, key_t const &right)
{
return left < right;
}
};
struct sb_interface
{
typedef double key_t;
typedef node_t node_t;
typedef value_node_t value_node_t;
static key_t const &get_key(value_node_t *node)
{
return node->score;
}
static bool is_nil(node_t *node)
{
return node->sb_is_nil;
}
static void set_nil(node_t *node, bool nil)
{
node->sb_is_nil = nil;
}
static node_t *get_parent(node_t *node)
{
return node->sb_parent;
}
static void set_parent(node_t *node, node_t *parent)
{
node->sb_parent = parent;
}
static node_t *get_left(node_t *node)
{
return node->sb_left;
}
static void set_left(node_t *node, node_t *left)
{
node->sb_left = left;
}
static node_t *get_right(node_t *node)
{
return node->sb_right;
}
static void set_right(node_t *node, node_t *right)
{
node->sb_right = right;
}
static size_t get_size(node_t *node)
{
return node->sb_size;
}
static void set_size(node_t *node, size_t size)
{
node->sb_size = size;
}
static bool predicate(key_t const &left, key_t const &right)
{
return left < right;
}
};
zzz::red_black_tree<rb_interface> root_;
zzz::red_black_tree<rb_interface> &key_tree()
{
return root_;
}
zzz::size_balanced_tree<sb_interface> &score_tree()
{
return *reinterpret_cast<zzz::size_balanced_tree<sb_interface> *>(&root_);
}
public:
redis_sorted_set()
{
::new(&root_) zzz::size_balanced_tree<sb_interface>();
}
~redis_sorted_set()
{
key_tree().safe_destroy([](node_t *node)
{
delete node;
});
key_tree().clear();
score_tree().clear();
score_tree().~size_balanced_tree<sb_interface>();
}
public:
template<class T, class = typename std::enable_if<std::is_constructible<std::string, T>::value>::type>
double zadd(T &&key, double score)
{
std::string key_find(std::forward<T>(key));
auto find = key_tree().find(key_find);
if (find == key_tree().end()) {
value_node_t *node = new value_node_t(std::move(key_find), score);
key_tree().insert(node);
score_tree().insert(node);
return score;
}
else {
score_tree().erase(&*find);
find->score = score;
score_tree().insert(&*find);
return find->score;
}
}
size_t zcard()
{
return key_tree().size();
}
size_t zcount(double min, double max)
{
return score_tree().count(min, max);
}
template<class T, class = typename std::enable_if<std::is_constructible<std::string, T>::value>::type>
double zincrby(T &&key, double score)
{
std::string key_find(std::forward<T>(key));
auto find = key_tree().find(key_find);
if (find == key_tree().end()) {
value_node_t *node = new value_node_t(std::move(key_find), score);
key_tree().insert(node);
score_tree().insert(node);
return score;
}
else {
score_tree().erase(&*find);
find->score += score;
score_tree().insert(&*find);
return find->score;
}
}
template<class F, class = typename std::enable_if<std::is_constructible<std::function<void(std::string const &, double)>, F>::value>::type>
void zrange(ptrdiff_t start, ptrdiff_t stop, F &&callback)
{
for (auto range = score_tree().slice(start, stop); range.first != range.second; ++range.first)
{
callback(range.first->key, range.first->score);
}
}
ptrdiff_t zrank(std::string const &key)
{
auto find = key_tree().find(key);
if (find == key_tree().end())
{
return -1;
}
return score_tree().rank(&*find);
}
size_t zrem(std::string const &key)
{
auto find = key_tree().find(key);
if (find == key_tree().end())
{
return 0;
}
key_tree().erase(find);
score_tree().erase(&*find);
delete &*find;
return 1;
}
double const *zscore(std::string const &key)
{
auto find = key_tree().find(key);
if (find == key_tree().end())
{
return nullptr;
}
return &find->score;
}
};