5 #ifndef STORAGE_LEVELDB_DB_SKIPLIST_H_ 6 #define STORAGE_LEVELDB_DB_SKIPLIST_H_ 32 #include "port/port.h" 40 template<
typename Key,
class Comparator>
111 return static_cast<int>(
112 reinterpret_cast<intptr_t
>(max_height_.NoBarrier_Load()));
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);
179 port::AtomicPointer next_[1];
182 template<
typename Key,
class Comparator>
186 sizeof(Node) +
sizeof(port::AtomicPointer) * (height - 1));
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>
213 template<
typename Key,
class Comparator>
224 template<
typename Key,
class Comparator>
229 template<
typename Key,
class Comparator>
234 template<
typename Key,
class Comparator>
242 template<
typename Key,
class Comparator>
245 static const unsigned int kBranching = 4;
255 template<
typename Key,
class Comparator>
258 return (n != NULL) && (
compare_(n->key, key) < 0);
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_
Iterator(const SkipList *list)
port::AtomicPointer max_height_
Node * NewNode(const Key &key, int height)
void SetNext(int n, Node *x)
void NoBarrier_SetNext(int n, Node *x)
bool Equal(const Key &a, const Key &b) const
char * AllocateAligned(size_t bytes)
Node * FindGreaterOrEqual(const Key &key, Node **prev) const
void Seek(const Key &target)
void operator=(const SkipList &)
bool KeyIsAfterNode(const Key &key, Node *n) const
bool Contains(const Key &key) const
SkipList(Comparator cmp, Arena *arena)
void Insert(const Key &key)
Node * FindLessThan(const Key &key) const
Node * NoBarrier_Next(int n)
Comparator const compare_