19
19
20
20
namespace container {
21
21
22
- template <typename Key, class Comparator = DefaultComparator<Key>>
22
+ template <typename Key, typename Value, class Comparator = DefaultComparator<Key>>
23
23
class LockFreeSkipList {
24
24
public:
25
25
const static int kMaxHeight {12 };
@@ -34,13 +34,13 @@ class LockFreeSkipList {
34
34
bool reset = false ,
35
35
Comparator cmp = DefaultComparator<Key>());
36
36
37
- bool Add (Key key);
37
+ bool Add (Key key, Value value );
38
38
bool Remove (Key key);
39
- Node* FindLessThan (Key key);
39
+ Value FindLessThan (Key key);
40
40
41
41
private:
42
42
int RandomHeight ();
43
- Node* NewNode (Key key, int height);
43
+ Node* NewNode (Key key, Value value, int height);
44
44
Node* GetNodeByOffset (uint32_t offset);
45
45
bool FindWindow (Key key,
46
46
Node** first_nodes,
@@ -55,11 +55,11 @@ class LockFreeSkipList {
55
55
Random rnd_;
56
56
Comparator compare_;
57
57
ShmManager* shm_manager_;
58
- LockFreeSkipList<Key, Comparator>::Node* head_;
58
+ LockFreeSkipList<Key, Value, Comparator>::Node* head_;
59
59
};
60
60
61
- template <typename Key, class Comparator >
62
- struct LockFreeSkipList <Key, Comparator>::Ref {
61
+ template <typename Key, typename Value, class Comparator >
62
+ struct LockFreeSkipList <Key, Value, Comparator>::Ref {
63
63
Ref () = default ;
64
64
Ref (uint32_t offset, bool mark): next_offset(offset), mark_removed(mark) {}
65
65
Ref (Node* node) {
@@ -72,11 +72,13 @@ struct LockFreeSkipList<Key, Comparator>::Ref {
72
72
char padding[3 ] {0 }; // 这里需要手工初始化字节对齐的字节
73
73
};
74
74
75
- template <typename Key, class Comparator >
76
- struct LockFreeSkipList <Key, Comparator>::Node {
77
- Node (int in_key, uint32_t in_offset, int in_height): height(in_height),
78
- key (in_key),
79
- self_offset(in_offset) {
75
+ template <typename Key, typename Value, class Comparator >
76
+ struct LockFreeSkipList <Key, Value, Comparator>::Node {
77
+ Node (Key in_key, Value in_value,
78
+ uint32_t in_offset, int in_height): height(in_height),
79
+ key (in_key),
80
+ value(in_value),
81
+ self_offset(in_offset) {
80
82
for (int i=0 ; i<kMaxHeight ; ++i) {
81
83
this ->ref [i] = {0 , false };
82
84
}
@@ -92,21 +94,22 @@ struct LockFreeSkipList<Key, Comparator>::Node {
92
94
93
95
int height;
94
96
Key key;
97
+ Value value;
95
98
uint32_t self_offset;
96
99
std::atomic<Ref> ref[kMaxHeight ];
97
100
};
98
101
99
- template <typename Key, class Comparator >
100
- LockFreeSkipList<Key, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
101
- bool reset,
102
- Comparator cmp):
102
+ template <typename Key, typename Value, class Comparator >
103
+ LockFreeSkipList<Key, Value, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
104
+ bool reset,
105
+ Comparator cmp):
103
106
// rnd_(static_cast<unsigned int>(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()))),
104
107
rnd_ (0xdeadbeef ),
105
108
compare_(cmp),
106
109
shm_manager_(shm_manager) {
107
110
108
111
if (reset) {
109
- this ->head_ = this ->NewNode (0 , kMaxHeight );
112
+ this ->head_ = this ->NewNode (Key (), Value () , kMaxHeight );
110
113
for (int i = 0 ; i < kMaxHeight ; i++) {
111
114
this ->head_ ->SetRef (i, {0 , false });
112
115
}
@@ -116,25 +119,25 @@ LockFreeSkipList<Key, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
116
119
117
120
}
118
121
119
- template <typename Key, class Comparator >
120
- auto LockFreeSkipList<Key, Comparator>::GetNodeByOffset(uint32_t offset) -> Node*{
122
+ template <typename Key, typename Value, class Comparator >
123
+ auto LockFreeSkipList<Key, Value, Comparator>::GetNodeByOffset(uint32_t offset) -> Node*{
121
124
if (offset == 0 ) {
122
125
return nullptr ;
123
126
} else {
124
127
return (Node*) this ->shm_manager_ ->GetBufferByOffset (offset);
125
128
}
126
129
}
127
130
128
- template <typename Key, class Comparator >
129
- auto LockFreeSkipList<Key, Comparator>::NewNode(Key key, int height) -> Node* {
131
+ template <typename Key, typename Value, class Comparator >
132
+ auto LockFreeSkipList<Key, Value, Comparator>::NewNode(Key key, Value value , int height) -> Node* {
130
133
uint32_t node_offset;
131
134
char * node_buffer;
132
135
std::tie (node_offset, node_buffer) = this ->shm_manager_ ->Allocate (sizeof (Node));
133
- return new (node_buffer) Node (key, node_offset, height);
136
+ return new (node_buffer) Node (key, value, node_offset, height);
134
137
}
135
138
136
- template <typename Key, class Comparator >
137
- bool LockFreeSkipList<Key, Comparator>::FindWindow(Key key,
139
+ template <typename Key, typename Value, class Comparator >
140
+ bool LockFreeSkipList<Key, Value, Comparator>::FindWindow(Key key,
138
141
Node** first_nodes,
139
142
Node** second_nodes) {
140
143
while (true ) {
@@ -148,8 +151,8 @@ bool LockFreeSkipList<Key, Comparator>::FindWindow(Key key,
148
151
}
149
152
}
150
153
151
- template <typename Key, class Comparator >
152
- auto LockFreeSkipList<Key, Comparator>::FindLessThan(Key key) -> Node* {
154
+ template <typename Key, typename Value, class Comparator >
155
+ Value LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) {
153
156
Node* pred = this ->head_ ;
154
157
for (int i=kMaxHeight -1 ; i>=0 ; --i) {
155
158
Node* curr = this ->GetNodeByOffset (pred->GetRef (i)->load ().next_offset );
@@ -175,12 +178,12 @@ auto LockFreeSkipList<Key, Comparator>::FindLessThan(Key key) -> Node* {
175
178
}
176
179
}
177
180
}
178
-
179
- return pred;
181
+
182
+ return pred-> value ;
180
183
}
181
184
182
- template <typename Key, class Comparator >
183
- bool LockFreeSkipList<Key, Comparator>::FindWindowInner(Node* start_node,
185
+ template <typename Key, typename Value, class Comparator >
186
+ bool LockFreeSkipList<Key, Value, Comparator>::FindWindowInner(Node* start_node,
184
187
Key key,
185
188
Node** first_nodes,
186
189
Node** second_nodes,
@@ -227,8 +230,8 @@ bool LockFreeSkipList<Key, Comparator>::FindWindowInner(Node* start_node,
227
230
return curr == nullptr ? false : (curr->key == key); // 这里已经进行到最底层, 如果找到key则返回true
228
231
}
229
232
230
- template <typename Key, class Comparator >
231
- int LockFreeSkipList<Key, Comparator>::RandomHeight() {
233
+ template <typename Key, typename Value, class Comparator >
234
+ int LockFreeSkipList<Key, Value, Comparator>::RandomHeight() {
232
235
233
236
static const unsigned int kBranching = 4 ;
234
237
int height = 1 ;
@@ -243,8 +246,8 @@ int LockFreeSkipList<Key, Comparator>::RandomHeight() {
243
246
// return u(this->rnd_);
244
247
}
245
248
246
- template <typename Key, class Comparator >
247
- bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
249
+ template <typename Key, typename Value, class Comparator >
250
+ bool LockFreeSkipList<Key, Value, Comparator>::Add(Key key, Value value ) {
248
251
int height = this ->RandomHeight ();
249
252
250
253
Node* first_nodes[kMaxHeight ];
@@ -261,7 +264,7 @@ bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
261
264
// 不支持重复的Key
262
265
return false ;
263
266
} else {
264
- Node* new_node = this ->NewNode (key, height);
267
+ Node* new_node = this ->NewNode (key, value, height);
265
268
for (int i=0 ; i<height; ++i) {
266
269
Ref ref_to_second = {second_nodes[i]};
267
270
new_node->SetRef (i, ref_to_second);
@@ -292,8 +295,8 @@ bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
292
295
}
293
296
}
294
297
295
- template <typename Key, class Comparator >
296
- bool LockFreeSkipList<Key, Comparator>::Remove(Key key) {
298
+ template <typename Key, typename Value, class Comparator >
299
+ bool LockFreeSkipList<Key, Value, Comparator>::Remove(Key key) {
297
300
Node* first_nodes[kMaxHeight ];
298
301
Node* second_nodes[kMaxHeight ];
299
302
while (true ) {
0 commit comments