forked from greensky00/skiplist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_set_example.cc
51 lines (41 loc) · 1.38 KB
/
cpp_set_example.cc
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
#include "sl_set.h"
#include <stdio.h>
int main() {
// sl_set: Busy-waiting implementation.
// erase() API may be blocked by concurrent
// operations dealing with iterator on the
// same key.
//
// sl_set_gc: Lazy reclaiming implementation.
// erase() API will not be blocked by
// any concurrent operations, but may
// consume more memory.
// sl_set<int> slist;
sl_set_gc<int> slist;
// << Insertion >>
// Insert 3 integers: 0, 1, and 2.
for (int i=0; i<3; ++i) {
slist.insert(i);
}
// << Point lookup >>
for (int i=0; i<3; ++i) {
auto itr = slist.find(i);
if (itr == slist.end()) continue; // Not found.
printf("[point lookup] %d\n", *itr);
// Note: In `sl_set`, while `itr` is alive and holding a node
// in skiplist, other thread cannot erase and free the node.
// Same as `shared_ptr`, `itr` will automatically release
// the node when it is not referred anymore.
// But if you want to release the node before that,
// you can do it as follows:
// itr = slist.end();
}
// << Erase >>
// Erase 1.
slist.erase(1);
// << Iteration >>
for (auto& entry: slist) {
printf("[iteration] %d\n", entry);
}
return 0;
}