-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsl.hpp
159 lines (137 loc) · 3.45 KB
/
sl.hpp
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
#ifndef SL_HPP
#define SL_HPP
#include "stdlib.h"
#include <urcu-defer.h>
#include <atomic>
#include <random>
#include <utility>
#include <iostream> // only for debug
#include "markable_ptr.hpp"
namespace nanahan {
namespace detail {
template <typename T>
void nothing_deleter(const T*) {
} // do nothing
} // namespace detail
template <typename KeyType, typename ValueType>
class LachesisDB {
private:
class node_t {
private:
friend class LachesisDB<KeyType, ValueType>;
static node_t* allocate(int size) {
const size_t calc_size =
sizeof(KeyType) +
sizeof(ValueType) +
sizeof(int) +
sizeof(markable_ptr<node_t>) * size;
const size_t aligned_size = ((calc_size + 7) / 8) * 8;
std::cout << "aligned_size:" << aligned_size << std::endl;
node_t* ret = static_cast<node_t*>(malloc(aligned_size));
ret->top_layer = size;
return ret;
}
static node_t* new_node(KeyType key, ValueType value, int height) {
node_t* ret = allocate(height);
new (&ret->first) KeyType(key);
new (&ret->second) ValueType(value);
return ret;
}
public:
const KeyType first;
ValueType second;
private:
int top_layer; // length
markable_ptr<node_t> next[1]; // variable length
};
public:
class iterator {
public:
node_t* point;
private:
iterator(node_t* p)
: point(p) {}
public:
~iterator() {}
node_t& operator*() { return *point; }
node_t* operator->() { return &operator*(); }
const node_t& operator*() const { return *point; }
const node_t* operator->() const { return &operator*();}
bool operator==(const iterator& rhs) const {
return point == rhs.point;
}
bool operator!=(const iterator& rhs) const {
return !this->operator==(rhs);
}
iterator& operator++() {
for (;;) {
if (!point->next[0]->is_null()) {
point = point->next[0];
}
}
return *this;
}
};
private:
const int max_height_;
mutable std::mt19937 rand_;
node_t* head_;
public:
LachesisDB(int max_height, int randomseed = std::random_device()())
: max_height_(max_height),
rand_(static_cast<unsigned long>(randomseed)) {
head_ = node_t::allocate(max_height);
std::cout << "max_height:" <<max_height << std::endl;
for (int i = 0; i < max_height; ++i) {
head_->next[i] = nullptr;
}
}
~LachesisDB() {
this->clear();
}
bool contains(const KeyType& k) {
}
iterator get(const KeyType& k) {
}
iterator lower_bound(const KeyType& k) {
}
bool add(const KeyType& k, const ValueType& v) {
}
iterator& begin() { return iterator(head_); }
const iterator& begin() const { return iterator(nullptr);}
iterator& end() { return nullptr; }
const iterator& end() const { return nullptr; }
bool remove(const KeyType& k) {
}
bool is_empty() const {
return head_ == nullptr;
}
void clear() {
node_t* next = head_;
while (next == nullptr) {
node_t* old_next = next;
next = next->next[0].get_ptr();
free(old_next);
}
}
public:
uint32_t random_level() const {
const uint32_t gen = rand_();
int bit = 1;
int cnt = 0;
while (cnt < max_height_ - 1) {
if (gen & bit) {
return cnt;
}
bit <<= 1;
++cnt;
}
return cnt;
}
private:
LachesisDB();
LachesisDB(const LachesisDB&);
LachesisDB& operator=(const LachesisDB&);
};
} // namespace nanahan
#endif