2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// Thread safety
|
|
|
|
// -------------
|
|
|
|
//
|
|
|
|
// Writes require external synchronization, most likely a mutex.
|
|
|
|
// Reads require a guarantee that the SkipList will not be destroyed
|
|
|
|
// while the read is in progress. Apart from that, reads progress
|
|
|
|
// without any internal locking or synchronization.
|
|
|
|
//
|
|
|
|
// Invariants:
|
|
|
|
//
|
|
|
|
// (1) Allocated nodes are never deleted until the SkipList is
|
|
|
|
// destroyed. This is trivially guaranteed by the code since we
|
|
|
|
// never delete any skip list nodes.
|
|
|
|
//
|
|
|
|
// (2) The contents of a Node except for the next/prev pointers are
|
|
|
|
// immutable after the Node has been linked into the SkipList.
|
|
|
|
// Only Insert() modifies the list, and it is careful to initialize
|
|
|
|
// a node and use release-stores to publish the nodes in one or
|
|
|
|
// more lists.
|
|
|
|
//
|
|
|
|
// ... prev vs. next pointer ordering ...
|
2012-10-19 21:00:53 +00:00
|
|
|
//
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2014-02-11 17:46:30 +00:00
|
|
|
#include "util/arena.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "port/port.h"
|
2014-01-31 01:18:17 +00:00
|
|
|
#include "util/arena.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/random.h"
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
|
|
|
class SkipList {
|
|
|
|
private:
|
|
|
|
struct Node;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Create a new SkipList object that will use "cmp" for comparing keys,
|
|
|
|
// and will allocate memory using "*arena". Objects allocated in the arena
|
|
|
|
// must remain allocated for the lifetime of the skiplist object.
|
2013-11-22 21:31:00 +00:00
|
|
|
explicit SkipList(Comparator cmp, Arena* arena,
|
|
|
|
int32_t max_height = 12, int32_t branching_factor = 4);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Insert key into the list.
|
|
|
|
// REQUIRES: nothing that compares equal to key is currently in the list.
|
|
|
|
void Insert(const Key& key);
|
|
|
|
|
|
|
|
// Returns true iff an entry that compares equal to key is in the list.
|
|
|
|
bool Contains(const Key& key) const;
|
|
|
|
|
|
|
|
// Iteration over the contents of a skip list
|
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
// Initialize an iterator over the specified list.
|
|
|
|
// The returned iterator is not valid.
|
|
|
|
explicit Iterator(const SkipList* list);
|
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
// Change the underlying skiplist used for this iterator
|
|
|
|
// This enables us not changing the iterator without deallocating
|
|
|
|
// an old one and then allocating a new one
|
|
|
|
void SetList(const SkipList* list);
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Returns true iff the iterator is positioned at a valid node.
|
|
|
|
bool Valid() const;
|
|
|
|
|
|
|
|
// Returns the key at the current position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
const Key& key() const;
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
void Next();
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
void Prev();
|
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
|
|
|
void Seek(const Key& target);
|
|
|
|
|
|
|
|
// Position at the first entry in list.
|
|
|
|
// Final state of iterator is Valid() iff list is not empty.
|
|
|
|
void SeekToFirst();
|
|
|
|
|
|
|
|
// Position at the last entry in list.
|
|
|
|
// Final state of iterator is Valid() iff list is not empty.
|
|
|
|
void SeekToLast();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkipList* list_;
|
|
|
|
Node* node_;
|
|
|
|
// Intentionally copyable
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-11-22 21:31:00 +00:00
|
|
|
const int32_t kMaxHeight_;
|
|
|
|
const int32_t kBranching_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Immutable after construction
|
|
|
|
Comparator const compare_;
|
|
|
|
Arena* const arena_; // Arena used for allocations of nodes
|
|
|
|
|
|
|
|
Node* const head_;
|
|
|
|
|
|
|
|
// Modified only by Insert(). Read racily by readers, but stale
|
|
|
|
// values are ok.
|
|
|
|
port::AtomicPointer max_height_; // Height of the entire list
|
|
|
|
|
2012-05-04 21:40:36 +00:00
|
|
|
// Used for optimizing sequential insert patterns
|
2013-11-22 21:31:00 +00:00
|
|
|
Node** prev_;
|
|
|
|
int32_t prev_height_;
|
2012-05-04 21:40:36 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
inline int GetMaxHeight() const {
|
2012-03-30 20:15:49 +00:00
|
|
|
return static_cast<int>(
|
|
|
|
reinterpret_cast<intptr_t>(max_height_.NoBarrier_Load()));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read/written only by Insert().
|
|
|
|
Random rnd_;
|
|
|
|
|
|
|
|
Node* NewNode(const Key& key, int height);
|
|
|
|
int RandomHeight();
|
|
|
|
bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
|
|
|
|
|
|
|
|
// Return true if key is greater than the data stored in "n"
|
|
|
|
bool KeyIsAfterNode(const Key& key, Node* n) const;
|
|
|
|
|
|
|
|
// Return the earliest node that comes at or after key.
|
2013-03-01 02:04:58 +00:00
|
|
|
// Return nullptr if there is no such node.
|
2011-03-18 22:37:00 +00:00
|
|
|
//
|
2013-03-01 02:04:58 +00:00
|
|
|
// If prev is non-nullptr, fills prev[level] with pointer to previous
|
2011-03-18 22:37:00 +00:00
|
|
|
// node at "level" for every level in [0..max_height_-1].
|
|
|
|
Node* FindGreaterOrEqual(const Key& key, Node** prev) const;
|
|
|
|
|
|
|
|
// Return the latest node with a key < key.
|
|
|
|
// Return head_ if there is no such node.
|
|
|
|
Node* FindLessThan(const Key& key) const;
|
|
|
|
|
|
|
|
// Return the last node in the list.
|
|
|
|
// Return head_ if list is empty.
|
|
|
|
Node* FindLast() const;
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
SkipList(const SkipList&);
|
|
|
|
void operator=(const SkipList&);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation details follow
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
struct SkipList<Key, Comparator>::Node {
|
2011-03-18 22:37:00 +00:00
|
|
|
explicit Node(const Key& k) : key(k) { }
|
|
|
|
|
|
|
|
Key const key;
|
|
|
|
|
|
|
|
// Accessors/mutators for links. Wrapped in methods so we can
|
|
|
|
// add the appropriate barriers as necessary.
|
|
|
|
Node* Next(int n) {
|
|
|
|
assert(n >= 0);
|
|
|
|
// Use an 'acquire load' so that we observe a fully initialized
|
|
|
|
// version of the returned Node.
|
|
|
|
return reinterpret_cast<Node*>(next_[n].Acquire_Load());
|
|
|
|
}
|
|
|
|
void SetNext(int n, Node* x) {
|
|
|
|
assert(n >= 0);
|
|
|
|
// Use a 'release store' so that anybody who reads through this
|
|
|
|
// pointer observes a fully initialized version of the inserted node.
|
|
|
|
next_[n].Release_Store(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No-barrier variants that can be safely used in a few locations.
|
|
|
|
Node* NoBarrier_Next(int n) {
|
|
|
|
assert(n >= 0);
|
|
|
|
return reinterpret_cast<Node*>(next_[n].NoBarrier_Load());
|
|
|
|
}
|
|
|
|
void NoBarrier_SetNext(int n, Node* x) {
|
|
|
|
assert(n >= 0);
|
|
|
|
next_[n].NoBarrier_Store(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Array of length equal to the node height. next_[0] is lowest level link.
|
|
|
|
port::AtomicPointer next_[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
typename SkipList<Key, Comparator>::Node*
|
|
|
|
SkipList<Key, Comparator>::NewNode(const Key& key, int height) {
|
2011-03-18 22:37:00 +00:00
|
|
|
char* mem = arena_->AllocateAligned(
|
|
|
|
sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1));
|
|
|
|
return new (mem) Node(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline SkipList<Key, Comparator>::Iterator::Iterator(const SkipList* list) {
|
2013-11-08 08:31:09 +00:00
|
|
|
SetList(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::SetList(const SkipList* list) {
|
2011-03-18 22:37:00 +00:00
|
|
|
list_ = list;
|
2013-03-01 02:04:58 +00:00
|
|
|
node_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline bool SkipList<Key, Comparator>::Iterator::Valid() const {
|
2013-03-01 02:04:58 +00:00
|
|
|
return node_ != nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline const Key& SkipList<Key, Comparator>::Iterator::key() const {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
return node_->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::Next() {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
node_ = node_->Next(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::Prev() {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Instead of using explicit "prev" links, we just search for the
|
|
|
|
// last node that falls before key.
|
|
|
|
assert(Valid());
|
|
|
|
node_ = list_->FindLessThan(node_->key);
|
|
|
|
if (node_ == list_->head_) {
|
2013-03-01 02:04:58 +00:00
|
|
|
node_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::Seek(const Key& target) {
|
2013-03-01 02:04:58 +00:00
|
|
|
node_ = list_->FindGreaterOrEqual(target, nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::SeekToFirst() {
|
2011-03-18 22:37:00 +00:00
|
|
|
node_ = list_->head_->Next(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
inline void SkipList<Key, Comparator>::Iterator::SeekToLast() {
|
2011-03-18 22:37:00 +00:00
|
|
|
node_ = list_->FindLast();
|
|
|
|
if (node_ == list_->head_) {
|
2013-03-01 02:04:58 +00:00
|
|
|
node_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
int SkipList<Key, Comparator>::RandomHeight() {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Increase height with probability 1 in kBranching
|
|
|
|
int height = 1;
|
2013-11-22 21:31:00 +00:00
|
|
|
while (height < kMaxHeight_ && ((rnd_.Next() % kBranching_) == 0)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
height++;
|
|
|
|
}
|
|
|
|
assert(height > 0);
|
2013-11-22 21:31:00 +00:00
|
|
|
assert(height <= kMaxHeight_);
|
2011-03-18 22:37:00 +00:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
bool SkipList<Key, Comparator>::KeyIsAfterNode(const Key& key, Node* n) const {
|
2013-03-01 02:04:58 +00:00
|
|
|
// nullptr n is considered infinite
|
|
|
|
return (n != nullptr) && (compare_(n->key, key) < 0);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::
|
|
|
|
FindGreaterOrEqual(const Key& key, Node** prev) const {
|
2012-05-04 21:40:36 +00:00
|
|
|
// Use prev as an optimization hint and fallback to slow path
|
|
|
|
if (prev && !KeyIsAfterNode(key, prev[0]->Next(0))) {
|
|
|
|
Node* x = prev[0];
|
|
|
|
Node* next = x->Next(0);
|
|
|
|
if ((x == head_) || KeyIsAfterNode(key, x)) {
|
2013-09-26 09:12:10 +00:00
|
|
|
// Adjust all relevant insertion points to the previous entry
|
|
|
|
for (int i = 1; i < prev_height_; i++) {
|
|
|
|
prev[i] = x;
|
|
|
|
}
|
2012-05-04 21:40:36 +00:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Normal lookup
|
2011-03-18 22:37:00 +00:00
|
|
|
Node* x = head_;
|
|
|
|
int level = GetMaxHeight() - 1;
|
|
|
|
while (true) {
|
|
|
|
Node* next = x->Next(level);
|
2013-09-26 09:12:10 +00:00
|
|
|
// Make sure the lists are sorted.
|
|
|
|
// If x points to head_ or next points nullptr, it is trivially satisfied.
|
|
|
|
assert((x == head_) || (next == nullptr) || KeyIsAfterNode(next->key, x));
|
2011-03-18 22:37:00 +00:00
|
|
|
if (KeyIsAfterNode(key, next)) {
|
|
|
|
// Keep searching in this list
|
|
|
|
x = next;
|
|
|
|
} else {
|
2013-03-01 02:04:58 +00:00
|
|
|
if (prev != nullptr) prev[level] = x;
|
2011-03-18 22:37:00 +00:00
|
|
|
if (level == 0) {
|
|
|
|
return next;
|
|
|
|
} else {
|
|
|
|
// Switch to next list
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
typename SkipList<Key, Comparator>::Node*
|
|
|
|
SkipList<Key, Comparator>::FindLessThan(const Key& key) const {
|
2011-03-18 22:37:00 +00:00
|
|
|
Node* x = head_;
|
|
|
|
int level = GetMaxHeight() - 1;
|
|
|
|
while (true) {
|
|
|
|
assert(x == head_ || compare_(x->key, key) < 0);
|
|
|
|
Node* next = x->Next(level);
|
2013-03-01 02:04:58 +00:00
|
|
|
if (next == nullptr || compare_(next->key, key) >= 0) {
|
2011-03-18 22:37:00 +00:00
|
|
|
if (level == 0) {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
// Switch to next list
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::FindLast()
|
2011-03-18 22:37:00 +00:00
|
|
|
const {
|
|
|
|
Node* x = head_;
|
|
|
|
int level = GetMaxHeight() - 1;
|
|
|
|
while (true) {
|
|
|
|
Node* next = x->Next(level);
|
2013-03-01 02:04:58 +00:00
|
|
|
if (next == nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
if (level == 0) {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
// Switch to next list
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
SkipList<Key, Comparator>::SkipList(const Comparator cmp, Arena* arena,
|
2013-11-22 21:31:00 +00:00
|
|
|
int32_t max_height,
|
|
|
|
int32_t branching_factor)
|
|
|
|
: kMaxHeight_(max_height),
|
|
|
|
kBranching_(branching_factor),
|
|
|
|
compare_(cmp),
|
2011-03-18 22:37:00 +00:00
|
|
|
arena_(arena),
|
2013-11-22 21:31:00 +00:00
|
|
|
head_(NewNode(0 /* any key will do */, max_height)),
|
2011-03-18 22:37:00 +00:00
|
|
|
max_height_(reinterpret_cast<void*>(1)),
|
2013-09-26 09:12:10 +00:00
|
|
|
prev_height_(1),
|
2011-03-18 22:37:00 +00:00
|
|
|
rnd_(0xdeadbeef) {
|
2013-11-22 21:31:00 +00:00
|
|
|
assert(kMaxHeight_ > 0);
|
|
|
|
assert(kBranching_ > 0);
|
|
|
|
// Allocate the prev_ Node* array, directly from the passed-in arena.
|
|
|
|
// prev_ does not need to be freed, as its life cycle is tied up with
|
|
|
|
// the arena as a whole.
|
|
|
|
prev_ = (Node**) arena_->AllocateAligned(sizeof(Node*) * kMaxHeight_);
|
|
|
|
for (int i = 0; i < kMaxHeight_; i++) {
|
2013-03-01 02:04:58 +00:00
|
|
|
head_->SetNext(i, nullptr);
|
2012-05-04 21:40:36 +00:00
|
|
|
prev_[i] = head_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
void SkipList<Key, Comparator>::Insert(const Key& key) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
|
|
|
|
// here since Insert() is externally synchronized.
|
2012-05-04 21:40:36 +00:00
|
|
|
Node* x = FindGreaterOrEqual(key, prev_);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Our data structure does not allow duplicate insertion
|
2013-03-01 02:04:58 +00:00
|
|
|
assert(x == nullptr || !Equal(key, x->key));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
int height = RandomHeight();
|
|
|
|
if (height > GetMaxHeight()) {
|
|
|
|
for (int i = GetMaxHeight(); i < height; i++) {
|
2012-05-04 21:40:36 +00:00
|
|
|
prev_[i] = head_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
//fprintf(stderr, "Change height from %d to %d\n", max_height_, height);
|
|
|
|
|
|
|
|
// It is ok to mutate max_height_ without any synchronization
|
|
|
|
// with concurrent readers. A concurrent reader that observes
|
|
|
|
// the new value of max_height_ will see either the old value of
|
2013-03-01 02:04:58 +00:00
|
|
|
// new level pointers from head_ (nullptr), or a new value set in
|
2011-03-18 22:37:00 +00:00
|
|
|
// the loop below. In the former case the reader will
|
2013-03-01 02:04:58 +00:00
|
|
|
// immediately drop to the next level since nullptr sorts after all
|
2011-03-18 22:37:00 +00:00
|
|
|
// keys. In the latter case the reader will use the new node.
|
|
|
|
max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
|
|
|
|
}
|
|
|
|
|
|
|
|
x = NewNode(key, height);
|
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
// NoBarrier_SetNext() suffices since we will add a barrier when
|
|
|
|
// we publish a pointer to "x" in prev[i].
|
2012-05-04 21:40:36 +00:00
|
|
|
x->NoBarrier_SetNext(i, prev_[i]->NoBarrier_Next(i));
|
|
|
|
prev_[i]->SetNext(i, x);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2012-05-04 21:40:36 +00:00
|
|
|
prev_[0] = x;
|
2013-09-26 09:12:10 +00:00
|
|
|
prev_height_ = height;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, class Comparator>
|
2014-03-10 19:56:46 +00:00
|
|
|
bool SkipList<Key, Comparator>::Contains(const Key& key) const {
|
2013-03-01 02:04:58 +00:00
|
|
|
Node* x = FindGreaterOrEqual(key, nullptr);
|
|
|
|
if (x != nullptr && Equal(key, x->key)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|