跳表的原理就是利用随机性建立索引,加速搜索,并且简化代码实现难度。具体的跳表原理不再赘述,主要是看了levelDB有一些实现细节的东西,凸显自己写的实现不足之处。

  • 去除冗余的key

      template<typename Key, class Comparator>
    struct SkipList<Key,Comparator>::Node {
    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];
    };

    这里使用一个Node节点表示所有相同key,不同高度的节点集合,仅保留了key和不同高度的向右指针,并且使用NewNode来动态分配随即高度的向右指针集合,而next_就指向这指针集合。这也是c/c++ tricky的地方。

      #include <stdio.h>
    struct Node {
    char str[1];
    };
    int main() {
    char* mem = new char[4];
    for (int i = 0; i < 4; i++) {
    mem[i] = i + '0';
    }
    Node* node = (Node*)mem;
    char* const pstr = node->str;
    for (int i = 0; i < 4; i++) {
    printf("%c", pstr[i]);
    }
    return 0;
    }

    就像上面这个简单的sample,成员str可以作为指针指向从数组下标0开始的元素,并且不受申明时的限制,不局限于大小1,索引至分配的最大的内存地址。

  • 简易随机数生成

      uint32_t Next() {
    static const uint32_t M = 2147483647L; // 2^31-1
    static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
    // We are computing
    // seed_ = (seed_ * A) % M, where M = 2^31-1
    //
    // seed_ must not be zero or M, or else all subsequent computed values
    // will be zero or M respectively. For all other values, seed_ will end
    // up cycling through every number in [1,M-1]
    uint64_t product = seed_ * A; // Compute (product % M) using the fact that ((x << 31) % M) == x.
    seed_ = static_cast<uint32_t>((product >> 31) + (product & M));
    // The first reduction may overflow by 1 bit, so we may need to
    // repeat. mod == M is not possible; using > allows the faster
    // sign-bit-based test.
    if (seed_ > M) {
    seed_ -= M;
    }
    return seed_;
    }

    可以看到,他使用A和M对种子进行运算,达到一定数据范围内不会重复的数集,而里面对于(product % M),使用(product >> 31) + (product & M)进行运算优化,考虑右移和与操作的代价远小于取余操作。

  • 简洁清晰的私有帮助方法,帮助寻找小于指定key的节点

      template<typename Key, class Comparator>
    typename SkipList<Key,Comparator>::Node*
    SkipList<Key,Comparator>::FindLessThan(const Key& key) const {
    Node* x = head_;
    int level = GetMaxHeight() - 1;
    while (true) {
    assert(x == head_ || compare_(x->key, key) < 0);
    Node* next = x->Next(level);
    if (next == NULL || compare_(next->key, key) >= 0) {
    if (level == 0) {
    return x;
    } else {
    // Switch to next list
    level--;
    }
    } else {
    x = next;
    }
    }
    }

levelDB跳表实现的更多相关文章

  1. LevelDB学习笔记 (3): 长文解析memtable、跳表和内存池Arena

    LevelDB学习笔记 (3): 长文解析memtable.跳表和内存池Arena 1. MemTable的基本信息 我们前面说过leveldb的所有数据都会先写入memtable中,在leveldb ...

  2. SkipList 跳表

    1.定义描述      跳跃列表(也称跳表)是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间).      基本上,跳跃列表是对有序的链表增加 ...

  3. [转载] 跳表SkipList

    原文: http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html leveldb中memtable的思想本质上是一个skiplist ...

  4. skiplist 跳表(1)

    最近学习中遇到一种新的数据结构,很实用,搬过来学习. 原文地址:skiplist 跳表   为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. ...

  5. SkipList跳表基本原理

    为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. 想象一下,给你一张草稿纸,一只笔,一个编辑器,你能立即实现一颗红黑树,或者AVL树 出来吗 ...

  6. C语言跳表(skiplist)实现

    一.简介 跳表(skiplist)是一个非常优秀的数据结构,实现简单,插入.删除.查找的复杂度均为O(logN).LevelDB的核心数据结构是用跳表实现的,redis的sorted set数据结构也 ...

  7. K:跳表

      跳表(SkipList)是一种随机化的数据结构,目前在redis和leveldb中都有用到它,它的效率和红黑树以及 AVL 树不相上下,但跳表的原理相当简单,只要你能熟练操作链表, 就能轻松实现一 ...

  8. SkipList跳表(一)基本原理

    一直听说跳表这个数据结构,说要学一下的,懒癌犯了,是该治治了 为什么选择跳表 目前经常使用的平衡数据结构有:B树.红黑树,AVL树,Splay Tree(这个树好像还没有听说过),Treep(也没有听 ...

  9. 深入理解跳表在Redis中的应用

    本文首发于:深入理解跳表在Redis中的应用微信公众号:后端技术指南针持续输出干货 欢迎关注 前面写了一篇关于跳表基本原理和特性的文章,本次继续介绍跳表的概率平衡和工程实现, 跳表在Redis.Lev ...

随机推荐

  1. strstr 的使用

    Problem E: Automatic Editing Source file: autoedit.{c, cpp, java, pas} Input file: autoedit.in Outpu ...

  2. bind和live

    <div> <ul> <li></li> <li></li> <li></li> <li>& ...

  3. HDU3535-AreYouBusy

    描述: As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, ...

  4. Java垃圾回收初步理解

    Java技术提供了一个系统级的线程,即垃圾收集器线程(线程优先级低),垃圾收集线程在一个Java程序中的执行是自动的,不能强制执行, 可以手动调用java.lang.System.gc(),通知JVM ...

  5. IOS 学习笔记(7) 控件 分隔栏控件(UISegmentControl)的使用方法

    分隔栏控件的系统默认式样一共有3种,分别是“普通式样”,"边框式样","条状式样" 分隔栏控件中有一个momentary属性,默认时NO.当开发者配置成YES时 ...

  6. mybatis日记配置Log4j

    拷贝log4j-1.2.16.jar到项目lib下 方式一是在src下新建一个log4j.xml文件,其具体内容如下 <?xml version="1.0" encoding ...

  7. The request failed with HTTP status 401: Unauthorized.

    Reporting Service 控件默认由IIS里面的应用程序池标识 里面所定义的用户连接,如果用户没有权限则报以下错误 The request failed with HTTP status 4 ...

  8. DBCC CHECKDB用法 手工修复数据库

          快速修复 DBCC CHECKDB ('数据库名', REPAIR_FAST)      重建索引并修复 DBCC CHECKDB ('数据库名', REPAIR_REBUILD) 如果必 ...

  9. 用nodejs安装hexo,将hexo部署到github

    跌跌撞撞写这篇博文,希望下一篇可以好点 运行环境:最新版本的nodejs + git 安装好nodejs 和 git ,注册好github账号,新建仓库****.github.io(****为gith ...

  10. IntelliJ IDEA 14 注册码生成器

    IntelliJ IDEA 14 注册码生成器 文件为Java代码 自己编译运行里面的程序输入名称然后就生成注册码了工具:http://yun.baidu.com/s/1cZKsA部分工具生成的注册码 ...