@@ -30,13 +30,15 @@ class LockFreeSkipList {
30
30
struct Node ;
31
31
struct Ref ;
32
32
public:
33
+ class Iterator ;
34
+
33
35
LockFreeSkipList (ShmManager* shm_manager,
34
36
bool reset = false ,
35
37
Comparator cmp = DefaultComparator<Key>());
36
38
37
39
bool Add (Key key, Value value);
38
40
bool Remove (Key key);
39
- Value FindLessThan (Key key);
41
+ Iterator FindLessThan (Key key);
40
42
41
43
private:
42
44
int RandomHeight ();
@@ -58,6 +60,34 @@ class LockFreeSkipList {
58
60
LockFreeSkipList<Key, Value, Comparator>::Node* head_;
59
61
};
60
62
63
+ template <typename Key, typename Value, class Comparator >
64
+ class LockFreeSkipList <Key, Value, Comparator>::Iterator {
65
+ public:
66
+ Iterator (LockFreeSkipList<Key, Value, Comparator>* skiplist,
67
+ Node* node): skiplist_(skiplist), node_(node) {}
68
+
69
+ bool Valid () const {
70
+ return this ->node_ ->GetRef (0 )->load ().mark_removed ;
71
+ }
72
+
73
+ const Key& key () const {
74
+ return this ->node_ ->key ;
75
+ }
76
+
77
+ const Value& value () const {
78
+ return this ->node_ ->value ;
79
+ }
80
+
81
+ void Next () {
82
+ this ->node_ = this ->skiplist_ ->GetNodeByOffset (this ->node_ ->GetRef (0 )->load ().next_offset );
83
+ }
84
+
85
+ private:
86
+ LockFreeSkipList<Key, Value, Comparator>* skiplist_;
87
+ Node* node_;
88
+ };
89
+
90
+
61
91
template <typename Key, typename Value, class Comparator >
62
92
struct LockFreeSkipList <Key, Value, Comparator>::Ref {
63
93
Ref () = default ;
@@ -152,7 +182,8 @@ bool LockFreeSkipList<Key, Value, Comparator>::FindWindow(Key key,
152
182
}
153
183
154
184
template <typename Key, typename Value, class Comparator >
155
- Value LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) {
185
+ // typename LockFreeSkipList<Key, Value, Comparator>::
186
+ auto LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) -> Iterator {
156
187
Node* pred = this ->head_ ;
157
188
for (int i=kMaxHeight -1 ; i>=0 ; --i) {
158
189
Node* curr = this ->GetNodeByOffset (pred->GetRef (i)->load ().next_offset );
@@ -179,7 +210,7 @@ Value LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) {
179
210
}
180
211
}
181
212
182
- return pred-> value ;
213
+ return Iterator ( this , pred) ;
183
214
}
184
215
185
216
template <typename Key, typename Value, class Comparator >
0 commit comments