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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
#pragma once #include <assert.h> #include <cstdint> #include <stdlib.h> #include <atomic> #include <random>
namespace ROCKSDB_NAMESPACE {
class Random { public: Random() : rng_(std::random_device{}()) {} static Random * Instance() { static Random rdm; return &rdm; } uint32_t Next() { return uni_(rng_); } private: Random(const Random &) = delete; Random& operator=(const Random &) = delete; Random(Random &&) = delete; Random& operator=(Random &&) = delete; Random *_instance = nullptr; std::mt19937 rng_; std::uniform_int_distribution<uint32_t> uni_; }; #define RANDOM (Random::Instance())
template<typename Key, class Comparator> class SkipList { private: struct Node;
public: explicit SkipList(Comparator cmp, int32_t max_height = 12, int32_t branching_factor = 4); SkipList(const SkipList&) = delete; void operator=(const SkipList&) = delete;
void Insert(const Key& key);
bool Contains(const Key& key) const;
uint64_t EstimateCount(const Key& key) const;
class Iterator { public: explicit Iterator(const SkipList* list);
void SetList(const SkipList* list);
bool Valid() const;
const Key& key() const;
void Next();
void Prev();
void Seek(const Key& target);
void SeekForPrev(const Key& target);
void SeekToFirst();
void SeekToLast();
private: const SkipList* list_; Node* node_; };
private: const uint16_t kMaxHeight_; const uint16_t kBranching_; const uint32_t kScaledInverseBranching_;
Comparator const compare_;
Node* const head_;
std::atomic<int> max_height_;
Node** prev_; int32_t prev_height_;
inline int GetMaxHeight() const { return max_height_.load(std::memory_order_relaxed); }
Node* NewNode(const Key& key, int height); int RandomHeight(); bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); } bool LessThan(const Key& a, const Key& b) const { return (compare_(a, b) < 0); }
bool KeyIsAfterNode(const Key& key, Node* n) const;
Node* FindGreaterOrEqual(const Key& key) const;
Node* FindLessThan(const Key& key, Node** prev = nullptr) const;
Node* FindLast() const; };
template<typename Key, class Comparator> struct SkipList<Key, Comparator>::Node { explicit Node(const Key& k) : key(k) { }
Key const key;
Node* Next(int n) { assert(n >= 0); return (next_[n].load(std::memory_order_acquire)); } void SetNext(int n, Node* x) { assert(n >= 0); next_[n].store(x, std::memory_order_release); }
Node* NoBarrier_Next(int n) { assert(n >= 0); return next_[n].load(std::memory_order_relaxed); } void NoBarrier_SetNext(int n, Node* x) { assert(n >= 0); next_[n].store(x, std::memory_order_relaxed); }
private: std::atomic<Node*> next_[1]; };
template<typename Key, class Comparator> typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::NewNode(const Key& key, int height) { char* mem = (char *)malloc( sizeof(Node) + sizeof(std::atomic<Node*>) * (height - 1)); return new (mem) Node(key); }
template<typename Key, class Comparator> inline SkipList<Key, Comparator>::Iterator::Iterator(const SkipList* list) { SetList(list); }
template<typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::SetList(const SkipList* list) { list_ = list; node_ = nullptr; }
template<typename Key, class Comparator> inline bool SkipList<Key, Comparator>::Iterator::Valid() const { return node_ != nullptr; }
template<typename Key, class Comparator> inline const Key& SkipList<Key, Comparator>::Iterator::key() const { assert(Valid()); return node_->key; }
template<typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::Next() { assert(Valid()); node_ = node_->Next(0); }
template<typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::Prev() { assert(Valid()); node_ = list_->FindLessThan(node_->key); if (node_ == list_->head_) { node_ = nullptr; } }
template<typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::Seek(const Key& target) { node_ = list_->FindGreaterOrEqual(target); }
template <typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::SeekForPrev( const Key& target) { Seek(target); if (!Valid()) { SeekToLast(); } while (Valid() && list_->LessThan(target, key())) { Prev(); } }
template <typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::SeekToFirst() { node_ = list_->head_->Next(0); }
template<typename Key, class Comparator> inline void SkipList<Key, Comparator>::Iterator::SeekToLast() { node_ = list_->FindLast(); if (node_ == list_->head_) { node_ = nullptr; } }
template<typename Key, class Comparator> int SkipList<Key, Comparator>::RandomHeight() { int height = 1; while (height < kMaxHeight_ && RANDOM->Next() < kScaledInverseBranching_) { height++; } assert(height > 0); assert(height <= kMaxHeight_); return height; }
template<typename Key, class Comparator> bool SkipList<Key, Comparator>::KeyIsAfterNode(const Key& key, Node* n) const { return (n != nullptr) && (compare_(n->key, key) < 0); }
template<typename Key, class Comparator> typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>:: FindGreaterOrEqual(const Key& key) const { Node* x = head_; int level = GetMaxHeight() - 1; Node* last_bigger = nullptr; while (true) { assert(x != nullptr); Node* next = x->Next(level); assert(x == head_ || next == nullptr || KeyIsAfterNode(next->key, x)); assert(x == head_ || KeyIsAfterNode(key, x)); int cmp = (next == nullptr || next == last_bigger) ? 1 : compare_(next->key, key); if (cmp == 0 || (cmp > 0 && level == 0)) { return next; } else if (cmp < 0) { x = next; } else { last_bigger = next; level--; } } }
template<typename Key, class Comparator> typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::FindLessThan(const Key& key, Node** prev) const { Node* x = head_; int level = GetMaxHeight() - 1; Node* last_not_after = nullptr; while (true) { assert(x != nullptr); Node* next = x->Next(level); assert(x == head_ || next == nullptr || KeyIsAfterNode(next->key, x)); assert(x == head_ || KeyIsAfterNode(key, x)); if (next != last_not_after && KeyIsAfterNode(key, next)) { x = next; } else { if (prev != nullptr) { prev[level] = x; } if (level == 0) { return x; } else { last_not_after = next; level--; } } } }
template<typename Key, class Comparator> typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::FindLast() const { Node* x = head_; int level = GetMaxHeight() - 1; while (true) { Node* next = x->Next(level); if (next == nullptr) { if (level == 0) { return x; } else { level--; } } else { x = next; } } }
template <typename Key, class Comparator> uint64_t SkipList<Key, Comparator>::EstimateCount(const Key& key) const { uint64_t count = 0;
Node* x = head_; int level = GetMaxHeight() - 1; while (true) { assert(x == head_ || compare_(x->key, key) < 0); Node* next = x->Next(level); if (next == nullptr || compare_(next->key, key) >= 0) { if (level == 0) { return count; } else { count *= kBranching_; level--; } } else { x = next; count++; } } }
template <typename Key, class Comparator> SkipList<Key, Comparator>::SkipList(const Comparator cmp, int32_t max_height, int32_t branching_factor) : kMaxHeight_(static_cast<uint16_t>(max_height)), kBranching_(static_cast<uint16_t>(branching_factor)), kScaledInverseBranching_(0xffffffff / kBranching_), compare_(cmp), head_(NewNode(0 , max_height)), max_height_(1), prev_height_(1) { assert(max_height > 0 && kMaxHeight_ == static_cast<uint32_t>(max_height)); assert(branching_factor > 0 && kBranching_ == static_cast<uint32_t>(branching_factor)); assert(kScaledInverseBranching_ > 0); prev_ = reinterpret_cast<Node**>( malloc(sizeof(Node*) * kMaxHeight_)); for (int i = 0; i < kMaxHeight_; i++) { head_->SetNext(i, nullptr); prev_[i] = head_; } }
template<typename Key, class Comparator> void SkipList<Key, Comparator>::Insert(const Key& key) { if (!KeyIsAfterNode(key, prev_[0]->NoBarrier_Next(0)) && (prev_[0] == head_ || KeyIsAfterNode(key, prev_[0]))) { assert(prev_[0] != head_ || (prev_height_ == 1 && GetMaxHeight() == 1));
for (int i = 1; i < prev_height_; i++) { prev_[i] = prev_[0]; } } else { FindLessThan(key, prev_); }
assert(prev_[0]->Next(0) == nullptr || !Equal(key, prev_[0]->Next(0)->key));
int height = RandomHeight(); if (height > GetMaxHeight()) { for (int i = GetMaxHeight(); i < height; i++) { prev_[i] = head_; }
max_height_.store(height, std::memory_order_relaxed); }
Node* x = NewNode(key, height); for (int i = 0; i < height; i++) { x->NoBarrier_SetNext(i, prev_[i]->NoBarrier_Next(i)); prev_[i]->SetNext(i, x); } prev_[0] = x; prev_height_ = height; }
template<typename Key, class Comparator> bool SkipList<Key, Comparator>::Contains(const Key& key) const { Node* x = FindGreaterOrEqual(key); if (x != nullptr && Equal(key, x->key)) { return true; } else { return false; } }
}
|