Skip to content

Commit 80554df

Browse files
committed
Add Iterator To Skiplist
1 parent 339526d commit 80554df

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

demo/cpp_demo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int main(int argc, const char * argv[]) {
2626

2727
Wolf<int, int> wolf_home("/Users/wingerted/test_shared_memory_dat", 10000000, true);
2828
wolf_home.Add(1, 1);
29+
wolf_home.Find(2);
2930

3031

3132
// // insert code here...

src/core/container/lockfree_skiplist.h

+34-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class LockFreeSkipList {
3030
struct Node;
3131
struct Ref;
3232
public:
33+
class Iterator;
34+
3335
LockFreeSkipList(ShmManager* shm_manager,
3436
bool reset = false,
3537
Comparator cmp = DefaultComparator<Key>());
3638

3739
bool Add(Key key, Value value);
3840
bool Remove(Key key);
39-
Value FindLessThan(Key key);
41+
Iterator FindLessThan(Key key);
4042

4143
private:
4244
int RandomHeight();
@@ -58,6 +60,34 @@ class LockFreeSkipList {
5860
LockFreeSkipList<Key, Value, Comparator>::Node* head_;
5961
};
6062

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+
6191
template<typename Key, typename Value, class Comparator>
6292
struct LockFreeSkipList<Key, Value, Comparator>::Ref {
6393
Ref() = default;
@@ -152,7 +182,8 @@ bool LockFreeSkipList<Key, Value, Comparator>::FindWindow(Key key,
152182
}
153183

154184
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 {
156187
Node* pred = this->head_;
157188
for (int i=kMaxHeight-1; i>=0; --i) {
158189
Node* curr = this->GetNodeByOffset(pred->GetRef(i)->load().next_offset);
@@ -179,7 +210,7 @@ Value LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) {
179210
}
180211
}
181212

182-
return pred->value;
213+
return Iterator(this, pred);
183214
}
184215

185216
template<typename Key, typename Value, class Comparator>

src/wolf.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class Wolf{
4848
}
4949
}
5050

51-
// void Find(const Data& data) {
52-
// this->inner_data_->FindLessThan(data);
53-
// }
51+
bool Find(const Index& index) {
52+
auto iter = this->inner_data_->FindLessThan(index);
53+
return iter.Valid();
54+
}
5455

5556
private:
5657
const std::string index_path_;

0 commit comments

Comments
 (0)