5 #ifndef STORAGE_LEVELDB_DB_SKIPLIST_H_ 6 #define STORAGE_LEVELDB_DB_SKIPLIST_H_ 40 template<
typename Key,
class Comparator>
111 return static_cast<int>(
146 template<
typename Key,
class Comparator>
158 return reinterpret_cast<Node*>(next_[n].Acquire_Load());
164 next_[n].Release_Store(x);
170 return reinterpret_cast<Node*>(next_[n].NoBarrier_Load());
174 next_[n].NoBarrier_Store(x);
182 template<
typename Key,
class Comparator>
185 char* mem = arena_->AllocateAligned(
187 return new (mem) Node(
key);
190 template<
typename Key,
class Comparator>
196 template<
typename Key,
class Comparator>
198 return node_ != NULL;
201 template<
typename Key,
class Comparator>
207 template<
typename Key,
class Comparator>
210 node_ = node_->Next(0);
213 template<
typename Key,
class Comparator>
218 node_ = list_->FindLessThan(node_->key);
219 if (node_ == list_->head_) {
224 template<
typename Key,
class Comparator>
226 node_ = list_->FindGreaterOrEqual(target, NULL);
229 template<
typename Key,
class Comparator>
231 node_ = list_->head_->Next(0);
234 template<
typename Key,
class Comparator>
236 node_ = list_->FindLast();
237 if (node_ == list_->head_) {
242 template<
typename Key,
class Comparator>
245 static const unsigned int kBranching = 4;
255 template<
typename Key,
class Comparator>
261 template<
typename Key,
class Comparator>
267 Node* next = x->Next(level);
272 if (prev != NULL) prev[level] = x;
283 template<
typename Key,
class Comparator>
290 Node* next = x->Next(level);
291 if (next == NULL ||
compare_(next->key,
key) >= 0) {
304 template<
typename Key,
class Comparator>
310 Node* next = x->Next(level);
324 template<
typename Key,
class Comparator>
331 for (
int i = 0; i < kMaxHeight; i++) {
332 head_->SetNext(i, NULL);
336 template<
typename Key,
class Comparator>
340 Node* prev[kMaxHeight];
341 Node* x = FindGreaterOrEqual(
key, prev);
344 assert(x == NULL || !Equal(
key, x->key));
346 int height = RandomHeight();
347 if (height > GetMaxHeight()) {
348 for (
int i = GetMaxHeight(); i < height; i++) {
360 max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
363 x = NewNode(
key, height);
364 for (
int i = 0; i < height; i++) {
367 x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
368 prev[i]->SetNext(i, x);
372 template<
typename Key,
class Comparator>
374 Node* x = FindGreaterOrEqual(
key, NULL);
375 if (x != NULL && Equal(
key, x->key)) {
384 #endif // STORAGE_LEVELDB_DB_SKIPLIST_H_ bool Valid() const
Definition: skiplist.h:197
uint64_t Key
Definition: skiplist_test.cc:15
uint32_t Next()
Definition: random.h:25
Node(const Key &k)
Definition: skiplist.h:148
Definition: autocompact_test.cc:11
Iterator(const SkipList *list)
Definition: skiplist.h:191
Definition: port_example.h:75
port::AtomicPointer max_height_
Definition: skiplist.h:108
Definition: skiplist.h:98
Node * NewNode(const Key &key, int height)
Definition: skiplist.h:184
int GetMaxHeight() const
Definition: skiplist.h:110
const SkipList * list_
Definition: skiplist.h:92
void SetNext(int n, Node *x)
Definition: skiplist.h:160
void Next()
Definition: skiplist.h:208
Random rnd_
Definition: skiplist.h:116
Arena *const arena_
Definition: skiplist.h:102
Node *const head_
Definition: skiplist.h:104
void NoBarrier_SetNext(int n, Node *x)
Definition: skiplist.h:172
bool Equal(const Key &a, const Key &b) const
Definition: skiplist.h:120
Node * FindGreaterOrEqual(const Key &key, Node **prev) const
Definition: skiplist.h:262
void Seek(const Key &target)
Definition: skiplist.h:225
void Prev()
Definition: skiplist.h:214
void operator=(const SkipList &)
Key const key
Definition: skiplist.h:150
Definition: lockedpool.h:48
const Key & key() const
Definition: skiplist.h:202
bool KeyIsAfterNode(const Key &key, Node *n) const
Definition: skiplist.h:256
void SeekToLast()
Definition: skiplist.h:235
bool Contains(const Key &key) const
Definition: skiplist.h:373
SkipList(Comparator cmp, Arena *arena)
Definition: skiplist.h:325
void Insert(const Key &key)
Definition: skiplist.h:337
Node * FindLessThan(const Key &key) const
Definition: skiplist.h:285
Node * node_
Definition: skiplist.h:93
void * NoBarrier_Load() const
Definition: port_win.cc:139
Node * NoBarrier_Next(int n)
Definition: skiplist.h:168
Definition: skiplist_test.cc:17
Node * Next(int n)
Definition: skiplist.h:154
Node * FindLast() const
Definition: skiplist.h:305
Definition: skiplist.h:147
Comparator const compare_
Definition: skiplist.h:101
void SeekToFirst()
Definition: skiplist.h:230
Definition: skiplist.h:41
int RandomHeight()
Definition: skiplist.h:243
Definition: skiplist.h:59